/* Сальдо — телефон и iOS (агент «Мобильная»). Подключён последним: только переопределения, значения — из tokens.css.
   Телефон = ширина ≤ 767 px. Вырезы — env(safe-area-inset-*), клавиатура — --kb (ставит mobile.js по visualViewport).
   Общая переменная для чужих прилипших снизу элементов: calc(var(--mobile-bottom, 0px) + …). */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --kb: 0px;
  --tabbar-h: 0px;
  --mobile-bottom: 0px;
  --dock-h: 68px;               /* строка ввода 56 + отступ 12 */
}

/* ───────── Везде (на десктопе вырезов нет — значения не меняются) ───────── */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; overscroll-behavior-x: none; }
.top { padding-left: max(var(--pad), var(--safe-left)); padding-right: max(var(--pad), var(--safe-right)); }
.pins { padding-left: max(var(--pad), var(--safe-left)); padding-right: max(var(--pad), var(--safe-right)); }
body[data-mode] .view { padding-left: max(var(--pad), var(--safe-left)); padding-right: max(var(--pad), var(--safe-right)); }
.cmd-dock { padding-left: max(var(--pad), var(--safe-left)); padding-right: max(var(--pad), var(--safe-right)); }
.modal-body, .menu, .cmd-stage { overscroll-behavior: contain; }
.m-tabbar, .m-menu-scrim { display: none; }

/* Касание: подсветку даёт :active, а не «залипший» :hover */
@media (hover: none) {
  .btn:hover { background: transparent; }
  .btn-primary:hover { background: var(--accent); filter: none; }
  .btn-ghost:hover { color: var(--fg-muted); background: transparent; }
  .btn:active { background: var(--surface-2); }
  .btn-primary:active { background: var(--accent); filter: brightness(.92); }
  .chip:not(.is-on):hover { border-color: var(--border); }
  .nav a:hover { color: var(--fg-muted); }
  .nav a[aria-current="page"]:hover { color: var(--fg); }
  .menu-item:hover { background: transparent; }
  .menu-item:active { background: var(--surface-2); }
  .cmd-bar:hover { border-color: var(--border-strong); }
  .cmd-mic:hover { background: transparent; color: var(--fg-muted); }
  .today-listen:hover { background: transparent; border-color: var(--border-strong); }
  .today-listen:active { background: var(--surface); }
  .today-row-link:hover { background: transparent; }
  .ingot-row:hover .ingot-row-obj .ingot { transform: none; }
  .ingot-row:active .ingot-row-obj .ingot { transform: translateY(-3px); }
  button.ingot-seg:hover .ingot-body, button.ingot-seg:hover .ingot-glass { transform: none; filter: none; }
  .ingot-handle:hover { transform: none; }
  .deal-pebble:hover .deal-stone { transform: none; }
  .deal-pebble:active .deal-stone { transform: translateY(-4px) rotate(-3deg); }
  .deal-back:hover, .deal-terms-btn:hover { background: transparent; }
  .deal-del:hover { background: transparent; }
  .toast .btn:hover { background: transparent; }
  .ap-btn-ghost:hover { background: transparent; }
  .assist-item:hover:not(.is-on) { background: transparent; }
}
/* Сочетания клавиш на касании не нужны */
@media (hover: none) and (pointer: coarse) { .cmd-keys { display: none; } }

/* ══════════════ Телефон ══════════════ */
@media (max-width: 767px) {
  :root { --tabbar-h: 52px; --mobile-bottom: calc(var(--tabbar-h) + var(--safe-bottom)); }
  /* клавиатура открыта: вкладки прячутся, прилипшее снизу встаёт над клавиатурой */
  :root.m-kb { --mobile-bottom: var(--kb); }

  body { overscroll-behavior-y: none; }
  /* без задержки двойного касания; :where — любая своя touch-action экрана (перетаскивание) важнее */
  :where(.btn, .chip, .seg-btn, .menu-item, .pin, .m-tab) { touch-action: manipulation; }
  .m-tabbar, .brand { -webkit-touch-callout: none; }
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]), select, textarea { font-size: max(16px, 1em); }
  .ledger-parent-select { font-size: var(--text-md); }
  .cmd-field-label { font-size: var(--text-sm); }

  /* ── Шапка: бренд · отменить · настройки (в «Полёте» — бренд · Выйти) ── */
  .top {
    flex-wrap: nowrap; gap: var(--space-1);
    min-height: calc(52px + var(--safe-top));
    padding-top: calc(var(--safe-top) + var(--space-1)); padding-bottom: var(--space-1);
  }
  .top .nav, #btn-flight { display: none; }
  .brand { min-height: var(--tap); }
  .brand b { display: inline; font-size: var(--text-lg); }
  .top-right { gap: 0; margin-right: calc(-1 * var(--space-2)); }
  .top-right .btn-icon { width: var(--tap); min-height: var(--tap); }
  #btn-land { min-height: var(--tap); margin-right: var(--space-2); }
  .pins { padding-bottom: 0; gap: var(--space-4); scroll-snap-type: x proximity; scroll-padding-inline: var(--pad); }
  .pin {
    flex: 0 0 auto; max-width: calc(100% - var(--space-4)); min-height: var(--tap); align-items: center;
    display: grid; grid-template-columns: auto minmax(0, 1fr) auto; white-space: normal;
    scroll-snap-align: start;
  }
  .pin-name { overflow-wrap: anywhere; }
  .pin-val { max-width: 46vw; overflow-wrap: anywhere; }

  /* ── Экран: снизу место под строку ввода и вкладки ── */
  body[data-mode] .view { padding-top: var(--space-4); padding-bottom: calc(var(--mobile-bottom) + var(--dock-h) + var(--space-8)); }
  body[data-mode="flight"] .view { padding-bottom: calc(var(--mobile-bottom) + var(--space-8)); }

  /* ── Строка ввода — над вкладками; при клавиатуре — над клавиатурой ── */
  .cmd-dock { bottom: var(--mobile-bottom); padding-bottom: var(--space-2); transition: bottom var(--duration-fast) var(--ease); }
  .cmd-dock::before { inset: calc(-1 * var(--space-6)) 0 0 0; }
  body.cmd-open .cmd-dock { bottom: 0; padding-bottom: max(var(--space-2), var(--safe-bottom)); }
  :root.m-kb body .cmd-dock { bottom: var(--kb); padding-bottom: var(--space-2); }
  .cmd-chips { margin-bottom: var(--space-2); }
  .cmd-stage { padding-top: calc(var(--safe-top) + var(--space-12)); padding-bottom: calc(max(var(--space-2), var(--safe-bottom)) + var(--dock-h) + var(--space-6)); }
  :root.m-kb .cmd-stage { bottom: var(--kb); padding-bottom: calc(var(--dock-h) + var(--space-4)); }

  /* ── Нижние вкладки ── */
  .m-tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 52;
    display: flex; align-items: stretch;
    height: calc(var(--tabbar-h) + var(--safe-bottom));
    padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
    background: color-mix(in srgb, var(--surface) 90%, transparent);
    -webkit-backdrop-filter: saturate(1.6) blur(20px); backdrop-filter: saturate(1.6) blur(20px);
    box-shadow: 0 -0.5px 0 var(--border-strong);
    transition: transform var(--duration-base) var(--ease), background var(--duration-slow) var(--ease);
    -webkit-user-select: none; user-select: none; touch-action: manipulation;
  }
  :root.m-kb .m-tabbar, body.cmd-open .m-tabbar { transform: translateY(110%); visibility: hidden; transition: transform var(--duration-fast) var(--ease), visibility 0s var(--duration-fast); }
  .m-tab {
    position: relative; flex: 1 1 0; min-width: 0; min-height: var(--tap);
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    padding: 4px 2px 2px; color: var(--fg-muted); text-decoration: none;
    font: 500 var(--text-xs)/1.15 var(--font-body); letter-spacing: 0; white-space: nowrap;
  }
  .m-tab span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
  .m-tab-ic { position: relative; display: grid; place-items: center; width: 56px; height: 28px; border-radius: var(--radius-pill); transition: background var(--duration-base) var(--ease); }
  .m-tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
  .m-tab[aria-current="page"] { color: var(--fg); }
  .m-tab[aria-current="page"] .m-tab-ic { background: var(--surface-3); }
  .m-tab[aria-current="page"] svg { stroke-width: 1.9; }
  .m-tab:active .m-tab-ic { background: var(--surface-2); }
  .m-tab[aria-current="page"]:active .m-tab-ic { background: var(--surface-3); }
  .m-tab:focus-visible { outline-offset: -4px; border-radius: var(--radius-md); }
  body[data-mode="flight"] .m-tabbar { background: color-mix(in srgb, var(--bg) 88%, transparent); }

  /* ── Уведомления: внизу над строкой ввода, не под вырезом и не на вкладках ── */
  #toasts {
    top: auto; left: max(var(--space-4), var(--safe-left)); right: max(var(--space-4), var(--safe-right)); transform: none;
    bottom: calc(var(--mobile-bottom) + var(--dock-h) + var(--space-3));
    flex-direction: column; align-items: center;
  }
  body[data-mode="flight"] #toasts { bottom: calc(var(--mobile-bottom) + var(--space-4)); }
  body.cmd-open #toasts { bottom: calc(max(var(--space-2), var(--safe-bottom)) + var(--dock-h) + var(--space-3)); }
  .toast { max-width: 100%; min-height: 52px; }
  .toast .btn { min-height: var(--tap); }

  /* ── Окна — листы снизу ── */
  #layer .modal-wrap { place-items: end stretch; padding: calc(var(--safe-top) + var(--space-3)) 0 0; }
  :root.m-kb #layer .modal-wrap { bottom: var(--kb); }
  #layer .modal {
    width: 100%; max-width: none;
    max-height: calc(100dvh - var(--safe-top) - var(--space-3));
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding-bottom: var(--safe-bottom);
    animation: m-sheet-up var(--duration-slow) var(--ease-out);
    will-change: transform;
  }
  :root.m-kb #layer .modal { max-height: calc(100dvh - var(--kb) - var(--safe-top) - var(--space-3)); padding-bottom: 0; }
  #layer .modal::before {
    content: ""; position: absolute; z-index: 1; top: 6px; left: 50%; width: 36px; height: 5px; margin-left: -18px;
    border-radius: var(--radius-pill); background: var(--border-strong); pointer-events: none;
  }
  #layer .modal-head { padding: var(--space-6) var(--space-3) var(--space-1) var(--space-6); min-height: 64px; touch-action: none; cursor: grab; }
  #layer .modal-head .btn:focus-visible { outline-offset: -2px; }
  #layer .modal-head h2 { font-size: var(--text-lg); }
  #layer .modal-head, #layer .modal-foot { flex: none; }
  #layer .modal-body { min-height: 0; padding: var(--space-2) var(--space-6) var(--space-4); -webkit-overflow-scrolling: touch; }
  #layer .modal-foot { padding: var(--space-3) var(--space-6) var(--space-4); flex-wrap: nowrap; }
  #layer .modal-foot .btn { flex: 1 1 0; min-width: 0; }
  #layer .modal-wrap.leave .modal { animation: m-sheet-down 200ms var(--ease) forwards; }
  #layer .modal.is-dragging { animation: none; transition: none; cursor: grabbing; }

  /* ── Меню — как action sheet ── */
  #layer .menu {
    left: max(var(--space-2), var(--safe-left)) !important; right: max(var(--space-2), var(--safe-right));
    top: auto !important; bottom: calc(var(--space-2) + max(var(--safe-bottom), var(--kb)));
    width: auto; min-width: 0; max-width: none;
    max-height: calc(100dvh - var(--safe-top) - var(--kb) - var(--space-8));
    padding: var(--space-2); border-radius: var(--radius-lg);
    animation: m-sheet-up var(--duration-base) var(--ease-out);
  }
  #layer .menu.leave { animation: m-sheet-down 160ms var(--ease) forwards; }
  #layer .menu-item { min-height: 52px; font-size: var(--text-md); padding: 6px var(--space-4); border-radius: var(--radius-md); }
  #layer .menu-head { padding: var(--space-3) var(--space-4) var(--space-1); }
  #layer .menu-item.m-menu-cancel { justify-content: center; font-weight: 600; }
  .m-menu-scrim { display: block; position: fixed; inset: 0; z-index: 69; background: var(--scrim); animation: fade var(--duration-base) var(--ease); }
  .m-menu-scrim.leave { opacity: 0; transition: opacity 160ms var(--ease); }

  /* ── Сенсорные цели ≥ 44 ── */
  .btn-sm, .btn-s, .chip, .seg-btn, .deals-filter .seg-btn, .set-modal .seg-btn { min-height: var(--tap); }
  .check { min-height: var(--tap); }
  .today-all, .ingot-shelf-more { min-height: var(--tap); }

  /* ── «Сегодня» ── */
  .today-hello { font-size: clamp(36px, 11vw, var(--text-3xl)); }
  .today-text { font-size: clamp(var(--text-lg), 5.6vw, var(--text-xl)); }
  .today-hero { padding-bottom: var(--space-8); gap: var(--space-6); }
  .today-card-title { font-size: clamp(var(--text-xl), 7vw, var(--text-2xl)); }
  .today-listen { min-height: var(--tap); }

  /* ── «Сделки»: слитки по ширине экрана ── */
  .deals-hero { font-size: clamp(var(--text-2xl), 11vw, 56px); }
  .deals-head { padding-top: var(--space-2); }
  .ingot-row-name b { font-size: var(--text-lg); }

  /* ── «Сделка» ── */
  .deal-name { font-size: clamp(var(--text-2xl), 9vw, var(--text-3xl)); text-wrap: balance; }
  .deal-sheet.is-bottom { z-index: 66; padding-top: var(--space-4); padding-bottom: calc(var(--space-6) + max(var(--safe-bottom), var(--kb))); box-shadow: var(--shadow-2), 0 0 0 1px var(--border); }
  .deal-sheet.is-bottom::before {
    content: ""; position: absolute; top: 6px; left: 50%; width: 36px; height: 5px; margin-left: -18px;
    border-radius: var(--radius-pill); background: var(--border-strong); pointer-events: none;
  }
  .deal-sheet.is-bottom .deal-sheet-head { touch-action: none; }
  .deal-sheet.is-bottom.is-dragging { animation: none; transition: none; }
  .deal-sheet.is-bottom.leave { animation: m-sheet-down 180ms var(--ease) forwards; }

  /* ── «Автопилот» ── */
  .ap-crumbs { flex-wrap: wrap; row-gap: 0; }
  .ap-svg .ap-rem-note { text-anchor: end; }
  .ap-hit { stroke-width: var(--tap); }   /* невидимая зона касания вдоль траектории */
}

/* Телефон, лёжа (низкая высота): вкладки компактнее — иконка и подпись в строку */
@media (max-width: 767px) and (max-height: 500px) {
  :root { --tabbar-h: 44px; }
  .m-tab { flex-direction: row; gap: 6px; }
  .m-tab-ic { width: 28px; }
  .m-tab[aria-current="page"] .m-tab-ic { background: transparent; }
  .top { min-height: calc(48px + var(--safe-top)); }
}

@keyframes m-sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes m-sheet-down { to { transform: translateY(100%); } }
