/* Lighten Up — phone & touch layer. Loaded after app.css; overrides only.
   Principles (2026 mobile UX): 3–5 tab destinations, ≥44pt targets, bottom sheets in the thumb zone,
   progressive disclosure (filters one layer down), content first, safe areas, no keyboard-only hints on touch. */

:root { --tabbar-h: calc(58px + env(safe-area-inset-bottom, 0px)); }

/* Shared list row (dashboard lists, hot leads, installs): full-width tap target with a press state. */
.list-row { display: flex; align-items: center; gap: 12px; padding: 10px 18px; border-bottom: 1px solid var(--line); color: inherit; transition: background var(--dur-1); }
.list-row:last-child { border-bottom: 0; }
.list-row:hover, .list-row:active { background: var(--surface-2); text-decoration: none; }

/* Floating "new" action — phones only (desktop keeps the header button). */
.fab { display: none; }
/* Pull-to-refresh indicator (installed app only; browsers keep their native gesture). */
.ptr { position: fixed; left: 50%; top: calc(var(--topbar-h) + 10px); z-index: 65; width: 38px; height: 38px; border-radius: 50%; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow-2); display: grid; place-items: center; opacity: 0; transform: translate(-50%, -70px); transition: transform var(--dur-2) var(--ease-out), opacity var(--dur-2); pointer-events: none; }
.ptr svg { width: 18px; height: 18px; color: var(--text-2); transition: transform var(--dur-2) var(--ease-out), color var(--dur-1); }
.ptr.pulling { opacity: 1; transform: translate(-50%, 0); }
.ptr.armed svg { transform: rotate(180deg); color: var(--accent-ink); }
.ptr.loading { opacity: 1; transform: translate(-50%, 0); }
.ptr.loading svg { animation: spin 0.8s linear infinite; color: var(--accent-ink); }

@media (max-width: 900px) {
  :root {
    --fs-xs: 12px; --fs-sm: 13px; --fs-md: 15px; --fs-lg: 17px; --fs-xl: 19px; --fs-2xl: 24px;
    --topbar-h: calc(52px + env(safe-area-inset-top, 0px)); --gutter: 16px;
  }
  html, body { height: 100dvh; }
  .app { min-height: 100dvh; }

  /* Top bar: frosted, safe-area aware */
  .topbar { height: var(--topbar-h); padding: env(safe-area-inset-top, 0px) 12px 0; gap: 8px;
    background: color-mix(in srgb, var(--bg) 80%, transparent); backdrop-filter: blur(18px) saturate(1.4); -webkit-backdrop-filter: blur(18px) saturate(1.4); }
  .search-trigger { height: 38px; border-radius: 12px; }

  /* Pages: tighter header, room for the tab bar, soft entrance */
  .page { padding: 14px var(--gutter) calc(var(--tabbar-h) + 28px); }
  .page-head { margin-bottom: 12px; gap: 8px 10px; align-items: center; }
  .page-head h1 { font-size: 22px; letter-spacing: -0.02em; }
  .page-head .sub { font-size: var(--fs-sm); margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  /* a subtitle made of chips/spans (campaign detail) stays a wrapping row; the 2-line clamp above turns it into one run-on line */
  .page-head .sub.row { display: flex; -webkit-line-clamp: unset; overflow: visible; }
  .page-head .actions { margin-left: 0; width: 100%; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: 8px; padding-bottom: 2px; }
  .page-head .actions::-webkit-scrollbar { display: none; }
  .page-head .actions > * { flex: 0 0 auto; }

  /* Stats: 2×2 instead of a tall single column */
  .grid.cols-4 { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 12px 14px; gap: 2px; }
  .stat__label { font-size: 11px; letter-spacing: 0.05em; }
  .stat__value { font-size: 22px; }
  .stat__sub { font-size: 12px; }
  .card { border-radius: 16px; }
  .card__head { padding: 12px 14px; }
  .card__body { padding: 14px; }
  .list-row { padding: 12px 14px; min-height: 56px; }
  .dash-grid { grid-template-columns: minmax(0, 1fr) !important; gap: 12px !important; }
  .dash-grid > .col { min-width: 0; }
  .dash-mini { grid-template-columns: repeat(3, minmax(0, 1fr)) !important; }
  .dash-mini .stat__value { font-size: 20px; }

  /* Filter rows scroll sideways instead of stacking */
  .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity; padding-bottom: 2px; }
  .chips::-webkit-scrollbar { display: none; }
  .chips .chip { flex: 0 0 auto; scroll-snap-align: start; }
  .page > .chips, .leads-chips > .chips { margin-inline: calc(-1 * var(--gutter)); padding-inline: var(--gutter); }
  .leads-chips { display: block; margin-bottom: 12px; }
  .segmented button { height: 34px; }
  .leads-bar { margin-bottom: 10px; }
  .leads-bar .leads-search { flex: 1 1 auto; }
  .leads-bar .leads-search .input { height: 42px; border-radius: 12px; }
  .leads-filter-btn { flex: 0 0 auto; height: 42px; border-radius: 12px; }
  .leads-filter-btn .badge { margin-left: 2px; }

  /* Board: one column per swipe, edge to edge */
  .kanban { gap: 10px; margin-inline: calc(-1 * var(--gutter)); padding-inline: var(--gutter); scroll-snap-type: x mandatory; scrollbar-width: none; min-height: 50dvh; }
  .kanban::-webkit-scrollbar { display: none; }
  .kanban__col { flex: 0 0 calc(100vw - 2 * var(--gutter) - 14px); scroll-snap-align: center; max-height: calc(100dvh - var(--topbar-h) - var(--tabbar-h) - 210px); }

  /* Tab bar: frosted, labelled, with a soft active pill and press feedback */
  .mobile-nav { height: var(--tabbar-h); padding: 6px 6px calc(env(safe-area-inset-bottom, 0px) + 4px);
    background: color-mix(in srgb, var(--surface) 84%, transparent); backdrop-filter: blur(20px) saturate(1.5); -webkit-backdrop-filter: blur(20px) saturate(1.5);
    border-top: 1px solid color-mix(in srgb, var(--line) 85%, transparent); }
  .mobile-nav a { gap: 2px; font-size: 10.5px; font-weight: 600; color: var(--text-3); -webkit-user-select: none; user-select: none; transition: color var(--dur-1); }
  .mobile-nav a .ico { position: relative; display: grid; place-items: center; width: 46px; height: 28px; border-radius: 10px; transition: background var(--dur-2) var(--ease-out), transform var(--dur-1); }
  .mobile-nav a:active .ico { transform: scale(0.9); }
  .mobile-nav a.active { color: var(--accent-ink); }
  .mobile-nav a.active .ico { background: var(--accent-soft); }
  .mobile-nav svg { width: 22px; height: 22px; }
  .mobile-nav .badge { position: absolute; top: -3px; right: 3px; }

  /* Floating new-lead button above the tab bar */
  .fab { display: grid; place-items: center; position: fixed; right: 16px; bottom: calc(var(--tabbar-h) + 14px); width: 56px; height: 56px; border-radius: 18px; border: 0;
    background: var(--accent); color: var(--text-inverse); box-shadow: 0 10px 28px rgba(127, 212, 255, 0.35), var(--shadow-2); z-index: 60; transition: transform var(--dur-1), box-shadow var(--dur-1); animation: pop var(--dur-2) var(--ease-out) both; }
  .fab:active { transform: scale(0.94); box-shadow: var(--shadow-1); }
  .fab svg { width: 26px; height: 26px; }

  /* Sheets: drawers and modals rise from the bottom with a grab handle; buttons in the thumb zone */
  .drawer { height: min(92dvh, 100dvh); border-radius: 20px 20px 0 0; border-top: 0; animation-fill-mode: backwards; transition: transform var(--dur-2) var(--ease-out); }
  .drawer::before, .modal__panel::before { content: ""; position: absolute; top: 8px; left: 50%; width: 40px; height: 5px; border-radius: 3px; background: var(--line-strong); transform: translateX(-50%); }
  .drawer__head { padding: 22px 16px 12px; touch-action: none; }
  .drawer__body { padding: 14px 16px; }
  .drawer__foot { padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px)); }
  .drawer__foot .btn { flex: 1; }
  .modal { padding: 0; align-items: end; }
  .modal__panel { position: relative; width: 100%; max-height: 92dvh; border-radius: 20px 20px 0 0; border-bottom: 0; animation: rise var(--dur-3) var(--ease-out) both; }
  .modal__head { padding: 24px 16px 10px; }
  .modal__body { padding: 4px 16px 16px; }
  .modal__foot { padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px)); border-radius: 0; }
  .modal__foot .btn { flex: 1; min-height: 46px; }
  /* Long sheets (new lead, filters, dialogs) scroll as one panel: keep the title/close and the
     Save/Cancel row pinned so the main button is always on screen, not at the end of the form. */
  .modal__head { position: sticky; top: 0; z-index: 2; background: var(--surface); }
  .modal__foot { position: sticky; bottom: 0; z-index: 2; }
  /* The sheet scrolls by touch on its own, and while any sheet is open the tab bar steps aside so
     nothing can sit over its Save/Book row (Nov 2 2026: Book quote visit was under the tabs). */
  .modal__panel { -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
  .app:has(.modal) > .mobile-nav { visibility: hidden; pointer-events: none; }
  .menu { position: fixed !important; left: 10px !important; right: 10px !important; top: auto !important; bottom: calc(10px + env(safe-area-inset-bottom, 0px)) !important; min-width: 0 !important; padding: 8px; border-radius: 18px; box-shadow: var(--shadow-3); animation: rise var(--dur-2) var(--ease-out) both; }
  .menu button, .menu a { padding: 13px 12px; font-size: 16px; border-radius: 12px; }
  /* The top bar's backdrop-filter makes it the containing block for fixed children, so its menus drop down instead */
  .topbar .menu { top: calc(100% + 6px) !important; bottom: auto !important; animation: pop var(--dur-2) var(--ease-out) both; }
  .notif-panel > .row:first-child { flex-wrap: wrap; gap: 8px; }
  .notif-panel > .row:first-child h3 { flex: 1 1 100%; }
  .notif-panel > .row:first-child .btn { margin-left: auto; }
  .sched__toolbar .row.right { flex-wrap: nowrap !important; overflow-x: auto; scrollbar-width: none; width: 100%; margin-left: 0; padding-bottom: 2px; }
  .sched__toolbar .row.right::-webkit-scrollbar { display: none; }
  .sched__toolbar .row.right .select { flex: 0 0 auto; }
  .inbox-page .inbox__rail { border-left: 0; border-right: 0; border-radius: 0; margin-inline: calc(-1 * var(--gutter)); }
  .toasts { left: 12px; right: 12px; bottom: calc(var(--tabbar-h) + 10px); }
  .toast { min-width: 0; max-width: none; }
  .palette { top: max(12px, env(safe-area-inset-top, 0px)); width: calc(100% - 20px); }
  .sidebar.open { height: 100dvh; padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
  .inbox .inbox__filters .chip { height: 32px; padding: 0 11px; font-size: var(--fs-sm); }
  .inbox .inbox__filterbtn { height: 32px; padding: 0 11px; font-size: var(--fs-sm); }
}

/* While typing, the tab bar and the + button get out of the way: iPhone lets fixed bars drift up into
   the middle of the screen while the keyboard is open, right over the field being typed in. */
@media (max-width: 900px) {
  body:has(:is(input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="button"]):not([type="submit"]), textarea, [contenteditable="true"]):focus) :is(.mobile-nav, .fab) { display: none; }
}

@media (max-width: 900px) and (prefers-reduced-motion: no-preference) {
  /* `backwards`, not `both`: a finished animation that keeps holding opacity/transform turns every
     .page into its own stacking context for good, and then every sheet, menu and save bar a page
     renders (z-index 80-95) is painted UNDER the tab bar (70) and the + button (60) - the Save
     button of a bottom sheet sat behind the tab bar, untappable. Nothing is held once it ends. */
  .page { animation: page-in var(--dur-2) var(--ease-out) backwards; }
}
@keyframes page-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
/* ...which also means nothing held maps in check any more: Leaflet gives its panes and controls
   z-index 400-1000, so a map would paint over the top bar, the tab bar and the + button. Each map is
   its own layer now - stacked with the page, under the bars. */
.leaflet-container, .lead-map-wrap, .area-picker { isolation: isolate; }

/* Touch devices of any size: bigger targets, no tap flash, no keyboard hints, no iOS input zoom */
@media (pointer: coarse) {
  * { -webkit-tap-highlight-color: transparent; }
  button, a, .chip, .nav-item, .kcard { touch-action: manipulation; }
  .btn { min-height: 40px; }
  .btn.sm { min-height: 36px; }
  .btn.xs { min-height: 30px; }
  .btn.icon { min-width: 40px; }
  .btn.icon.sm { min-width: 36px; }
  .input, .select, .textarea { font-size: 16px; }
  .input, .select { min-height: 44px; }
  .input.sm, .select.sm { min-height: 40px; }
  .chip { height: 34px; padding: 0 13px; }
  .check input { width: 20px; height: 20px; }
  .switch { width: 46px; height: 28px; }
  .switch::after { width: 22px; height: 22px; }
  .switch[aria-checked="true"]::after { transform: translateX(18px); }
  .nav-item { padding: 11px 12px; }
  .table td { padding: 13px 12px; }
  .tabs button { height: 44px; }
  .kbd, .hide-touch { display: none !important; }
  .kcard { cursor: default; }
  .kcard:hover { transform: none; }

  /* iPhone zooms the whole page into any field whose text is under 16px, and stays zoomed - the
     lead drawer's 20-odd small fields, the inbox/calls search and the schedule filters all did it.
     !important because the page stylesheets (.ld-controls .select, .stepper input) load after this one. */
  .app :is(input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]), select, textarea) { font-size: 16px !important; }

  /* Thumb-sized targets where it matters most (Apple's 44pt): the top bar, sheet close buttons,
     the dashboard's round "complete task" button, and the smallest chips and buttons. */
  .topbar .btn.icon { min-width: 44px; min-height: 44px; }
  .search-trigger { min-height: 42px; }
  .drawer__head .btn.icon, .modal__head .btn.icon { min-width: 44px; min-height: 44px; }
  .list-row > .btn.icon.sm { min-width: 44px; min-height: 44px; }
  .btn.xs { min-height: 34px; }
  .segmented button { min-height: 38px; }
  .composer__quick .chip { height: 34px; padding: 0 12px; }
  .tpl__tokens .chip { height: 32px; }
  /* Text links in card headers ("All tasks →"): same look, a taller invisible hit area. */
  .card__head a { display: inline-block; padding: 12px 0; margin: -12px 0; }
}

/* Installed app (Home Screen): no rubber-banding of the shell; pull-to-refresh is ours */
@media (display-mode: standalone) {
  body { overscroll-behavior-y: none; }
}

/* Full-screen template picker. A regular .menu in the inbox is contained by
   .app { position: fixed }, so it sits under the top bar and the tab bar:
   the last rows are untappable and a tap near the top can close the list
   without inserting a template. */
@media (max-width: 900px) {
  .tpl-sheet {
    position: fixed;
    inset: 0;
    z-index: 120;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .tpl-sheet__head {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 52px;
    padding: 6px 10px;
    border-bottom: 1px solid var(--line);
  }
  .tpl-sheet__head h2 { margin: 0; font-size: 17px; font-weight: 700; }
  .tpl-sheet__list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 8px 8px calc(20px + env(safe-area-inset-bottom, 0px));
  }
  .tpl-sheet__item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 48px;
    padding: 13px 12px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    text-align: left;
    color: var(--text);
    font-size: 16px;
  }
  .tpl-sheet__item:active { background: var(--surface-2); }
  .app:has(.tpl-sheet) > .main { position: relative; z-index: 80; }
  .app:has(.tpl-sheet) > .mobile-nav { display: none !important; visibility: hidden; pointer-events: none; }
}
