/* Phone layout (<= 760px, the same threshold as `isPhone` in js/state.js). Loaded after style.css and overrides it:
   a bottom tab bar instead of the top menu, sheets instead of centred windows, cards instead of table rows.
   Tap targets are >= 40px, fields are 16px (iOS zooms into smaller ones), nothing relies on hover. */

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
a, button, input, textarea, label, .pill, .menu-item, .ui-select-btn { touch-action: manipulation; }
body { -webkit-tap-highlight-color: transparent; }

.tabbar { display: none; }
.sheet-grab { display: none; }
.sheet-scrim { display: none; }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes bar-up { from { opacity: 0; transform: translateY(10px); } }

@media (max-width: 760px) {
  :root { --tabbar-h: calc(58px + env(safe-area-inset-bottom, 0px)); --top-h: calc(52px + env(safe-area-inset-top, 0px)); }
  body { min-height: 100dvh; }
  input[type=text], input[type=password], input[type=email], input[type=search], input[type=number], textarea, .toolbar input[type=text] { font-size: 16px; }

  /* ---------- top bar: logo mark, cabinet switch, sync dot, avatar ---------- */
  .topbar { height: var(--top-h); padding: env(safe-area-inset-top, 0px) 10px 0; gap: 8px; }
  .topbar-left { gap: 0; }
  .topbar-left nav { display: none; }
  .topbar-right { flex: 1; min-width: 0; gap: 6px; justify-content: flex-end; }
  .account-switch { order: -1; flex: 1; min-width: 0; position: static; }
  .switch-btn { width: 100%; max-width: none; padding: 4px 10px 4px 5px; border-radius: 12px; min-height: 40px; }
  .switch-btn-text { display: flex; flex: 1; min-width: 0; }
  .switch-btn-name { max-width: none; }
  .switch-btn .chev { display: block; }
  .switch-menu { left: 8px; right: 8px; width: auto; top: calc(100% + 6px); max-height: calc(100dvh - var(--top-h) - var(--tabbar-h) - 16px); overflow: auto; }
  .switch-item { min-height: 48px; }
  .switch-link { padding: 10px 8px; }
  .sync-pill { min-width: 40px; min-height: 40px; justify-content: center; }
  .user-btn { padding: 2px; min-width: 40px; min-height: 40px; align-items: center; justify-content: center; }
  .user-menu .dropdown-menu { min-width: min(260px, calc(100vw - 20px)); }
  .user-menu .menu-item { min-height: 44px; align-items: center; }
  .org-tag { display: none; }

  /* ---------- bottom tab bar ---------- */
  .tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 45; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
    height: var(--tabbar-h); padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--tabbar-bg); backdrop-filter: saturate(180%) blur(14px); -webkit-backdrop-filter: saturate(180%) blur(14px);
    border-top: 1px solid var(--border);
  }
  .tabbar a {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; min-width: 0;
    color: var(--muted); font-size: 11px; font-weight: 500; text-decoration: none; position: relative;
  }
  .tabbar a:hover { text-decoration: none; }
  .tabbar a .ic { width: 23px; height: 23px; font-size: 23px; }
  .tabbar a span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabbar a.active { color: var(--accent-text); }
  .tabbar a:active { background: var(--hover); }
  .tabbar a.tab-main i { display: grid; place-items: center; width: 46px; height: 46px; margin-top: -16px; border-radius: 50%; background: var(--accent-solid); color: var(--on-accent); box-shadow: 0 0 0 4px var(--surface); }
  .tabbar a.tab-main .ic { width: 24px; height: 24px; }
  .tabbar a.tab-main span { margin-top: -1px; }
  .tabbar a.tab-main.active i { background: var(--accent-hover); }
  /* the virtual keyboard shrinks the viewport on Android: the bar would jump above it */
  body.typing .tabbar { display: none; }
  body.typing { --tabbar-h: env(safe-area-inset-bottom, 0px); }

  /* ---------- page ---------- */
  main { padding: 14px 12px calc(var(--tabbar-h) + 20px); }
  .page-head { flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
  .page-head h1 { font-size: 22px; }
  .page-head .btn-row { width: 100%; }
  .btn { min-height: 40px; }
  .btn-sm { min-height: 36px; }
  .btn-icon, .kebab { min-height: 40px; min-width: 40px; justify-content: center; }
  .btn-link { padding: 8px 0; }
  .btn-create { width: 100%; justify-content: center; }
  .auth-page { min-height: 100dvh; }
  .modal-scroll { max-height: calc(100dvh - 48px); }

  /* floating things sit above the tab bar */
  .bulk-fab { bottom: calc(var(--tabbar-h) + 10px); max-width: calc(100vw - 24px); }
  #toasts { left: 12px; right: 12px; bottom: calc(var(--tabbar-h) + 10px); align-items: stretch; }
  .toast { max-width: none; }
  .create-footer { bottom: calc(var(--tabbar-h) + 8px); }
  .table-card > .pager { bottom: var(--tabbar-h); }

  /* ---------- dashboard ---------- */
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 12px; }
  .kpi { padding: 10px 12px 9px; border-radius: 12px; }
  .kpi-label { margin-bottom: 3px; }
  .kpi-value { font-size: 18px; }
  .kpi-sub { margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .dchart-card { display: none; } /* the chart is not shown on a phone */
  .page-head > a[href="#/create"] { display: none; }

  /* filter bar: search on top, then the period and the filters as strips that scroll sideways */
  .toolbar { padding: 10px 12px; gap: 8px; border-radius: 12px; }
  .toolbar-row { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -12px; padding: 0 12px; scroll-snap-type: x proximity; scroll-padding-inline: 12px; }  /* snap to the padding, not the card's edge */
  .toolbar-row::-webkit-scrollbar { display: none; }
  .toolbar-row + .toolbar-row { padding-top: 8px; }
  .toolbar-row > * { flex: none; scroll-snap-align: start; }
  .toolbar-row .grow { display: none; }
  .toolbar-row:first-child { flex-wrap: wrap; overflow: visible; margin: 0; padding: 0; }
  .toolbar input[type=text].search { order: -2; flex: 1 0 100%; width: 100%; min-width: 0; height: 40px; border-radius: 10px; background-position: 13px center; padding-left: 36px; }
  .toolbar-row:first-child .dropdown { order: -1; }
  .toolbar-row:first-child .pills { flex: 1 1 0; min-width: 0; overflow-x: auto; scrollbar-width: none; }
  .toolbar-row:first-child .pills::-webkit-scrollbar { display: none; }
  .toolbar .btn-sm { height: 40px; }
  .pills { height: 40px; max-width: 100%; overflow-x: auto; scrollbar-width: none; }
  .pills::-webkit-scrollbar { display: none; }
  .pill { padding: 0 12px; flex: none; }
  .date-btn { max-width: 56vw; }
  .date-btn span:nth-child(2) { display: none; }
  .filter-btn.one { padding-left: 8px; }
  .range-chips { gap: 6px; }

  /* list of ads on a phone: a view switch, then either the table in its own scrolling frame or a compact list of cards */
  .phone-head { display: flex; flex-direction: column; gap: 8px; padding: 0 2px 8px; }
  .phone-head-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
  .cards-count { color: var(--muted); font-size: 13px; }
  .phone-count { display: inline-flex; align-items: center; gap: 12px; flex-wrap: wrap; }
  .phone-count .filter-reset { min-height: 40px; }
  .toolbar .filter-reset, .toolbar .filter-count { display: none; }  /* the filter row scrolls sideways on a phone: the line above the list has them */
  .cards-sort { width: 150px; flex: none; }
  .cards-sort .ui-select-btn { padding: 9px 10px; }
  .view-switch { display: inline-flex; padding: 3px; gap: 2px; border-radius: 11px; background: var(--track); flex: none; }
  .view-switch button { display: grid; place-items: center; width: 44px; height: 38px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); cursor: pointer; }
  .view-switch button.active { background: var(--seg-on); color: var(--accent-strong); box-shadow: 0 1px 3px rgba(var(--shadow-rgb), .14); }

  /* the table: scrolls inside its frame, the header row and the first column stay put */
  .table-card > .table-wrap { max-height: calc(100dvh - var(--top-h) - var(--tabbar-h) - 130px); min-height: 160px; overflow: auto; overscroll-behavior: contain; }
  table.items-grid th { height: 40px; padding: 0 10px; font-size: 12px; }
  table.items-grid td { height: 48px; padding: 0 10px; font-size: 13px; }
  table.items-grid tr.summary td { position: sticky; top: 40px; z-index: 1; height: 40px; }
  table.items-grid tr.summary td.check-col, table.items-grid tr.summary td.title-cell { z-index: 3; }
  table.items-grid .actions-cell, table.items-grid .item-id { display: none; }
  table.grid .check-col { width: 38px; min-width: 38px; max-width: 38px; }
  table.grid .title-cell, table.grid .title-col {
    left: 38px; width: calc(min(40vw, 170px) - 38px); min-width: calc(min(40vw, 170px) - 38px); max-width: calc(min(40vw, 170px) - 38px);
    white-space: normal; box-shadow: 2px 0 4px -2px rgba(var(--shadow-rgb), .16);
  }
  table.items-grid .item-title { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; line-height: 1.25; font-size: 13px; }
  table.items-grid td.title-cell { text-overflow: clip; }
  /* campaigns: no tick column, so the name sticks to the edge */
  table.camps-grid .title-cell, table.camps-grid .title-col { left: 0; width: min(40vw, 170px); min-width: min(40vw, 170px); max-width: min(40vw, 170px); }
  table.camps-grid .item-id { display: block; font-size: 11.5px; }
  table.camps-grid tr.summary td { top: 40px; }

  /* the cards: one box each, with room to breathe */
  .cards-all { display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; margin: -4px 0 -4px -6px; cursor: pointer; }
  .cards-all input, .item-check input { width: 22px; height: 22px; }
  .item-cards { display: flex; flex-direction: column; gap: 12px; }
  .item-card { display: flex; gap: 2px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 6px 14px 6px 4px; min-width: 0; }
  .item-card.selected { background: var(--row-selected); border-color: var(--accent-line); }
  .item-total { background: var(--surface-2); padding: 12px 16px; }
  .item-card.no-check { padding-left: 16px; }
  .item-check { flex: none; display: flex; align-items: flex-start; justify-content: center; width: 40px; padding-top: 14px; cursor: pointer; }
  .item-card-main { flex: 1; min-width: 0; padding: 14px 0 10px; display: flex; flex-direction: column; gap: 10px; }
  .item-card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
  .item-card-top .item-title { font-size: 15px; line-height: 1.3; min-width: 0; overflow-wrap: anywhere; }
  .item-card-top .badge { flex: none; }
  .item-card .item-id { margin: -4px 0 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .item-metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; padding: 12px 0 4px; border-top: 1px solid var(--border-soft); }
  .item-metric { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
  .item-metric-v { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
  .item-metric-k { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .item-card-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
  .item-card-facts { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; color: var(--muted); min-width: 0; }
  .item-card .row-actions { opacity: 1; gap: 8px; }
  .item-card .row-actions .btn { border-color: var(--border-soft); }
  .pager-phone { display: flex; align-items: center; gap: 8px; margin-top: 8px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); padding: 6px 8px; }
  .pager-phone .pager-where { flex: 1; text-align: center; font-size: 13px; color: var(--text); }
  .pager-phone .btn { background: var(--btn-bg); }
  .pager-size { width: 104px; flex: none; }

  /* "bulk edit" is a bar above the tab bar */
  .bulk-fab { left: 10px; right: 10px; bottom: calc(var(--tabbar-h) + 8px); max-width: none; transform: none; animation: bar-up .16s ease-out; }
  .bulk-main { flex: 1; justify-content: center; height: 48px; padding-left: 14px; }
  .bulk-clear { height: 48px; width: 48px; }
  main:has(.bulk-fab) { padding-bottom: calc(var(--tabbar-h) + 84px); }

  /* ---------- sheets: menus slide up from the bottom ---------- */
  .dropdown-menu.sheet {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; width: auto; min-width: 0; max-width: none; max-height: 78dvh; overflow: auto;
    border: none; border-radius: 18px 18px 0 0; z-index: 70; padding: 24px 8px calc(10px + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(var(--track-strong), var(--track-strong)) no-repeat center 10px / 38px 4px, var(--pop); box-shadow: 0 -10px 40px rgba(var(--shade-rgb), .2);
    animation: sheet-up .2s ease-out; overscroll-behavior: contain;
  }
  .dropdown-menu.sheet:not(.hidden) + .sheet-scrim { display: block; position: fixed; inset: 0; z-index: 69; background: var(--overlay-sheet); animation: fade-in .15s ease-out; }
  .dropdown-menu.sheet .menu-item, .dropdown-menu.sheet label { min-height: 48px; display: flex; align-items: center; gap: 10px; font-size: 15px; white-space: normal; }
  .dropdown-menu.sheet .filter-item { padding: 8px 12px; }
  .dropdown-menu.sheet .filter-box { width: 22px; height: 22px; }
  .dropdown-menu.sheet input[type=checkbox] { width: 22px; height: 22px; }
  .dropdown-menu.sheet .kebab + .menu-item { white-space: normal; }
  .dropdown-menu.calendar.sheet { padding: 24px 12px calc(12px + env(safe-area-inset-bottom, 0px)); }
  .calendar.sheet .cal-field { min-height: 48px; font-size: 15px; }
  .calendar.sheet .cal-wd { color: var(--text-2); }  /* the sheet is lighter than the page: muted falls below AA in the dark theme */
  .calendar.sheet .cal-month, .dt-pop.sheet .cal-month { width: 100%; }
  .calendar.sheet .cal-grid { grid-template-columns: repeat(7, 1fr); }
  .calendar.sheet .cal-day { height: 40px; font-size: 14px; }
  .calendar.sheet .cal-nav { width: 40px; height: 40px; }
  .dt-foot input.dt-time { width: 96px; padding: 10px 8px; }
  .ui-select-btn, .dt-btn { min-height: 44px; }
  .ms-box { min-height: 44px; }

  /* ---------- windows: a sheet from the bottom, the big ones fill the screen ---------- */
  .modal-backdrop { padding: 0; align-items: flex-end; overflow: hidden; background: var(--overlay-sheet); }
  .modal-backdrop.modal-center { padding: 16px; align-items: center; }
  .modal-backdrop:not(.modal-center) > .modal {
    max-width: none; width: 100%; max-height: 92dvh; overflow-y: auto; overscroll-behavior: contain; border-radius: 18px 18px 0 0; box-shadow: none;
    padding: 6px 18px calc(18px + env(safe-area-inset-bottom, 0px)); animation: sheet-up .22s ease-out;
  }
  .sheet-grab { display: flex; justify-content: center; padding: 6px 0 12px; margin: 0 -18px; touch-action: none; }
  .sheet-grab i { width: 38px; height: 4px; border-radius: 2px; background: var(--track-strong); }
  .modal-backdrop:has(> .modal-scroll) { padding: 0; align-items: stretch; }
  .modal-backdrop > .modal.modal-scroll { max-width: none; width: 100%; height: 100dvh; max-height: 100dvh; border-radius: 0; padding: 0; animation: sheet-up .22s ease-out; }
  .modal-scroll .modal-head { padding: calc(14px + env(safe-area-inset-top, 0px)) 16px 12px; }
  .modal-scroll .modal-body { padding: 0 16px 20px; }
  .modal-item .modal-body { padding: 14px 16px 24px; }
  .modal-scroll .modal-foot { padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px)); flex-wrap: wrap; }
  .modal-item .modal-foot .btn-row { flex-wrap: wrap; width: 100%; }
  .modal-scroll .modal-body .table-wrap { overflow-x: auto; }
  .modal-close { min-width: 44px; min-height: 44px; margin: -8px -10px 0 0; }
  .modal-tabs { overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 12px; }
  .modal-tabs::-webkit-scrollbar { display: none; }
  .modal-tabs button { flex: none; min-height: 44px; }
  .modal .row3 { grid-template-columns: 1fr; gap: 0; }
  .panel .row3 > .field:last-child { margin-bottom: 16px; }
  /* the table scrolls sideways, so it is its own scroller: sticky inside it keeps the header and the totals row in view */
  .modal-item .modal-body .table-wrap { max-height: calc(100dvh - 250px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)); overflow: auto; overscroll-behavior: contain; }
  .modal-item .modal-body table.grid th { top: 0; }
  .kv { grid-template-columns: 110px minmax(0, 1fr); }
  .modal.job-window { height: min(86dvh, 720px); }
  .modal-md .result-list { max-height: 45dvh; }
  .confirm-actions { flex-direction: column-reverse; }
  .confirm-actions .btn { width: 100%; min-height: 44px; }
  .modal-sm { padding: 20px; }
  .be-head { min-height: 48px; flex-wrap: wrap; }
  .be-body > input, .be-body > textarea, .be-body > .input-prefix, .be-row > input { max-width: none; }
  .be-body .ui-select, .be-body .dt-input { max-width: none; }
  .modal-scroll .modal-foot .be-foot-left { flex-wrap: wrap; }
  .modal-scroll .modal-foot .btn-row { width: 100%; }
  .modal-scroll .modal-foot .btn-row .btn { flex: 1; justify-content: center; }

  /* ---------- create form ---------- */
  .create-target { flex-direction: column; align-items: stretch; gap: 6px; width: 100%; }
  .create-target .seg-sm { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .seg-sm button { min-height: 40px; }
  .create-grid, .create-col { gap: 12px; }
  .panel { padding: 14px; border-radius: 12px; }
  .seg { grid-template-columns: repeat(2, 1fr); }
  .seg button { min-height: 44px; }
  .preview-msg, .preview-bot { max-width: 100%; }
  .create-footer { flex-direction: column; align-items: stretch; gap: 10px; padding: 12px; }
  .create-footer .actions { width: 100%; justify-content: space-between; }
  .create-footer .btn-create { flex: 1; padding: 12px 16px; justify-content: center; }
  .schedule { overflow-x: auto; padding-bottom: 4px; }
  .schedule td { width: 24px; height: 28px; }
  .list-field textarea { font-size: 14px; }
  .list-meta .btn-link { padding: 8px 0; }
  .token-chip { padding: 6px 10px; }
  .input-prefix > input { min-height: 44px; }
  input[type=text], input[type=password], input[type=email], textarea { min-height: 44px; }

  /* ---------- automations and the rule editor ---------- */
  .auto-card { padding: 12px; }
  .auto-card-head { gap: 8px; }
  .auto-row { flex-wrap: wrap; }
  .auto-fields { flex-basis: 100%; }
  .auto-kw-cell { width: auto; flex: none; }
  .auto-cond-group .auto-add, .auto-note { margin-left: 0; }
  .auto-val { width: 100%; flex: 1 1 100px; }
  .auto-row input.auto-num { width: 100%; }
  .rule-name-row { flex-wrap: wrap; }
  .rule-summary-foot { flex-direction: column-reverse; }
  .rule-summary-foot .btn { width: 100%; justify-content: center; }
  .template-grid { grid-template-columns: 1fr; }
  .auto-toolbar { flex-wrap: wrap; }

  /* ---------- cabinets, team, billing, profile ---------- */
  .acc-head-row { display: none; }
  .acc-row { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px 10px; padding: 16px 14px; }
  .acc-head-row + .acc-row { border-top: none; border-radius: 15px 15px 0 0; }
  .acc-row > .acc-name, .acc-row > .acc-pay, .acc-row > .acc-actions { grid-column: 1 / -1; }
  .acc-num { text-align: left; }
  .acc-cell-label { display: block; }
  .acc-actions .btn { flex: 1; justify-content: center; }
  .price-grid { grid-template-columns: 1fr 1fr; }
  .pay-methods { grid-template-columns: minmax(0, 1fr); }
  .pay-actions .btn { flex: 1 1 100%; justify-content: center; }
  .profile-row { flex-wrap: wrap; gap: 8px 12px; }
  .profile-row-text { flex: 1 1 calc(100% - 60px); }
  .profile-row .btn, .profile-row .role-select { margin-left: 48px; }
  .profile-row .role-select { flex: 1 1 140px; width: auto; }
  .profile-name-form { flex-direction: column; align-items: stretch; }
  .profile-name-form input { min-width: 0; }
  .profile-wrap .panel > a.btn { display: flex; justify-content: center; }
  .profile-wrap .btn-row .btn { flex: 1 1 auto; justify-content: center; }
  .admin-wrap { max-width: none; }
  .admin-two { grid-template-columns: 1fr; }
  .admin-head-main { flex-direction: column; align-items: flex-start; }
  .admin-actions .btn { flex: 1 1 auto; justify-content: center; }
  .admin-toolbar .toolbar-row { overflow: visible; margin: 0; padding: 0; flex-wrap: wrap; }
  .admin-toolbar .toolbar-row .search { order: 0; flex: 1 0 100%; }
  .admin-seg { overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .admin-seg button { flex: none; min-height: 36px; }
  .admin-grid th:first-child, .admin-grid td:first-child { position: sticky; left: 0; z-index: 2; background: var(--surface); box-shadow: 2px 0 4px -2px rgba(var(--shadow-rgb), .16); }
  .admin-grid th:first-child { background: var(--surface-2); z-index: 3; }
  .admin-grid td:first-child { min-width: 150px; max-width: 46vw; }
  .imp-bar { padding: 8px 12px; }
  .tabs { overflow-x: auto; scrollbar-width: none; }
  .tabs a { flex: none; padding: 12px 14px; }
  .foot-note { margin-bottom: 8px; }
}


@media (max-width: 760px) and (prefers-reduced-motion: reduce) {
  .dropdown-menu.sheet, .modal-backdrop:not(.modal-center) > .modal, .modal.modal-scroll, .bulk-fab, .sheet-scrim { animation: none !important; }
}
