/* Phone layout. Everything here sits behind a max-width media query, so desktop and tablet are untouched.
   Loaded after styles.css so equal-specificity rules win. mobile.js builds #mobile-tabs, #nav-backdrop and #nav-close. */

#mobile-tabs, #nav-backdrop, #nav-close, .m-quick, .m-seg { display: none; }

@media (max-width: 768px) {
  html { -webkit-text-size-adjust: 100%; }
  body { overscroll-behavior-y: none; -webkit-tap-highlight-color: transparent; }
  body.nav-open { overflow: hidden; }

  /* ---- Menu: the sidebar becomes a slide-out drawer with full labels instead of a 60px icon strip ---- */
  #sidebar {
    width: min(86vw, 320px); z-index: 90; transform: translateX(-104%); transition: transform .22s ease;
    padding-top: max(18px, env(safe-area-inset-top)); padding-bottom: calc(18px + env(safe-area-inset-bottom));
    box-shadow: 0 0 44px rgba(0, 0, 0, .55); overscroll-behavior: contain;
  }
  body.nav-open #sidebar { transform: none; }
  #sidebar .brand { display: block; padding: 0 22px 14px; }
  #sidebar-logo { width: 150px; }
  #sidebar .nav-items a, #sidebar > a.nav-link, #sidebar .nav-label { min-height: 44px; }
  #sidebar .nav-items a, #sidebar > a.nav-link { font-size: 15px; }
  #sidebar a#logout-link { min-height: 44px; }
  .theme-toggle { min-height: 40px; }
  #nav-backdrop {
    display: block; position: fixed; inset: 0; z-index: 85; background: rgba(0, 0, 0, .55);
    opacity: 0; pointer-events: none; transition: opacity .2s ease;
  }
  body.nav-open #nav-backdrop { opacity: 1; pointer-events: auto; }
  #nav-close {
    display: flex; align-items: center; justify-content: center; position: absolute;
    top: max(10px, env(safe-area-inset-top)); right: 12px; width: 40px; height: 40px; border-radius: 50%;
    background: transparent; border: 1px solid var(--border); color: var(--ink); font-size: 24px; line-height: 1; cursor: pointer;
  }

  /* ---- Bottom tab bar ---- */
  #mobile-tabs {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 70;
    height: calc(58px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--bg-elev); border-top: 1px solid var(--border);
  }
  #app.hidden ~ #mobile-tabs, #app.hidden ~ #nav-backdrop { display: none; }
  #mobile-tabs > a, #mobile-tabs > button {
    flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    background: none; border: 0; color: var(--muted); text-decoration: none; font: inherit; font-size: 10.5px; font-weight: 600;
    letter-spacing: .2px; cursor: pointer;
  }
  #mobile-tabs svg { width: 22px; height: 22px; }
  #mobile-tabs .active { color: var(--ink); }
  #mobile-tabs .active svg { color: var(--mint); }

  /* ---- Page area ---- */
  #main { margin-left: 0; max-width: 100%; padding: 58px 14px calc(78px + env(safe-area-inset-bottom)); }
  .page-title { font-size: 22px; }
  #notif-wrap { top: max(8px, env(safe-area-inset-top)); right: 10px; }
  #notif-panel { position: fixed; top: calc(max(8px, env(safe-area-inset-top)) + 48px); left: 10px; right: 10px; width: auto; max-height: 70vh; }
  #toast { left: 12px; right: 12px; bottom: calc(70px + env(safe-area-inset-bottom)); }

  /* ---- Forms: 16px stops iOS zooming the page when a field is tapped; 44px is a comfortable thumb target ---- */
  input, select, textarea { font-size: 16px !important; }
  input:not([type="checkbox"]):not([type="radio"]), select { min-height: 44px; }
  .btn, button.btn { min-height: 42px; }

  /* ---- Sheets: modals slide up from the bottom and use the full width ---- */
  #modal-overlay { padding: 0; align-items: flex-end; z-index: 100; }
  #lightbox-overlay { z-index: 110; }
  #toast { z-index: 120; }
  #modal, #modal.modal-composer {
    width: 100%; max-width: 100%; max-height: 94vh; border-radius: 18px 18px 0 0; border-bottom: 0;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  }
  .modal-actions { position: sticky; bottom: calc(-20px - env(safe-area-inset-bottom)); background: var(--bg-elev); padding: 12px 0 4px; margin-top: 16px; }
  .modal-actions .btn { flex: 1; }
  #lightbox-overlay { padding: 12px; }

  /* ---- Two-column pages stack. Several grids set their column widths inline (e.g. "1fr 340px"), which beats the
     stylesheet's own stacking rule and made the whole page wider than the screen. ---- */
  .detail-grid[style*="1fr 340px"], .detail-grid[style*="1fr 380px"] { grid-template-columns: minmax(0, 1fr) !important; }
  .detail-grid > *, .masonry-grid > * { min-width: 0; }
  body, #app, #main { overflow-x: clip; }

  /* ---- Menu: every section is open, so each page is one tap away ---- */
  #sidebar .nav-group.collapsed .nav-items { display: flex; }
  #sidebar .nav-caret { display: none; }
  #sidebar .nav-label { cursor: default; pointer-events: none; min-height: 0; padding-top: 18px; padding-bottom: 6px; }

  /* ---- Dashboard numbers: an odd last card spans the row instead of leaving a hole ---- */
  .stats > .stat:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 14px; }
  .stat .num { font-size: 24px; }

  /* ---- Tabs rows scroll sideways instead of overflowing ---- */
  .sm-tabs { overflow-x: auto; scrollbar-width: none; white-space: nowrap; margin-left: -14px; margin-right: -14px; padding: 0 14px; }
  .sm-tabs::-webkit-scrollbar { display: none; }
  .sm-tab { flex: 0 0 auto; }

  /* ---- Toolbars: fields fill the row, nothing sticks out ---- */
  #main .toolbar select, #main .toolbar input { max-width: 100%; min-width: 0; }
  #main .toolbar .grow { flex: 1 1 100%; }
  .toolbar.m-filter { display: none; }
  .m-filters-open .toolbar.m-filter { display: flex; }
  .toolbar.m-filter select { flex: 1 1 100%; }
  .m-filter-btn.on { border-color: var(--mint); color: var(--mint); }

  /* ---- Floating add button for the page's primary "+ Add" action ---- */
  .m-fab {
    position: fixed; right: 16px; bottom: calc(74px + env(safe-area-inset-bottom)); z-index: 65;
    min-height: 48px; padding: 0 20px; border-radius: 26px; font-size: 15px; font-weight: 700;
    box-shadow: 0 8px 22px rgba(229, 9, 20, .42);
  }

  /* ---- Contact header: name on top, Email as a full-width button, the rest in one row underneath ---- */
  .toolbar:has(> #contact-btn) > .grow { display: none; }
  #contact-btn { flex: 1 1 100%; order: 5; min-height: 46px; font-size: 15px; }
  #add-to-pipeline-btn, #edit-contact, #del-contact { flex: 1 1 0; min-width: 0; order: 6; padding-left: 6px; padding-right: 6px; font-size: 13px; }
  .kv .v a { color: var(--blue); text-decoration: none; word-break: break-all; }
  .kv .v a:active { opacity: .6; }

  /* ---- Account page: Call / Website / Directions row, equal Edit and Delete, empty fields hidden ---- */
  .m-quick { display: flex; gap: 8px; margin: -2px 0 14px; }
  .m-quick .btn { flex: 1 1 0; display: flex; align-items: center; justify-content: center; min-height: 46px; text-decoration: none; font-weight: 700; }
  .toolbar:has(> #edit-church) > .grow { display: none; }
  .toolbar:has(> #edit-church) > .page-title { flex: 1 1 100%; }
  #edit-church, #del-church { flex: 1 1 0; min-width: 0; }
  .kv.m-empty { display: none; }

  /* Section headers inside a card ("Contacts  + Add Contact") keep the button on the heading's row */
  #main .card > .toolbar { flex-wrap: nowrap; }
  #main .card > .toolbar > .grow { flex: 1 1 0; }
  #main .card > .toolbar .btn { flex: 0 0 auto; min-height: 38px; padding: 0 14px; font-size: 13px; }

  /* ---- Accounts list cards: details as quiet lines, contacts and open deals side by side ---- */
  #main table.m-cards .m-sfx { display: none; }
  #main table.m-cards td[data-label="Affiliation"],
  #main table.m-cards td[data-label="Location"],
  #main table.m-cards td[data-label="Website"] { justify-content: flex-start; text-align: left; color: var(--muted); }
  #main table.m-cards td[data-label="Affiliation"]::before,
  #main table.m-cards td[data-label="Location"]::before,
  #main table.m-cards td[data-label="Website"]::before { display: none; }
  #main table.m-cards td[data-label="Contacts"] { grid-column: 1; justify-content: flex-start; gap: 8px; }
  #main table.m-cards td[data-label="Open Deals"] { grid-column: 2; justify-content: flex-start; gap: 8px; justify-self: end; }

  /* ---- Email list: title and status, sent date, three number tiles, then Duplicate / Delete ---- */
  #main table.m-cards.m-nl tr { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  #main table.m-cards.m-nl td.m-title { grid-column: 1 / 3; grid-row: 1; padding-bottom: 0; }
  #main table.m-cards.m-nl td.m-badge { grid-column: 3; grid-row: 1; }
  #main table.m-cards.m-nl td[data-label="Sent"] { grid-column: 1 / -1; grid-row: 2; justify-content: flex-start; color: var(--muted); font-size: 12.5px; padding: 0; }
  #main table.m-cards.m-nl td[data-label="Sent"]::before { content: "Sent "; text-transform: none; letter-spacing: 0; font-size: 12.5px; font-weight: 600; padding: 0; }
  #main table.m-cards.m-nl td[data-label="Recipients"], #main table.m-cards.m-nl td[data-label="Opens"], #main table.m-cards.m-nl td[data-label="Clicks"] {
    grid-column: auto; flex-direction: column-reverse; align-items: center; justify-content: center; gap: 2px; padding: 8px 4px;
    background: var(--bg); border-radius: 10px; text-align: center; font-size: 20px; font-weight: 800; color: var(--ink);
  }
  #main table.m-cards.m-nl td[data-label="Recipients"]::before, #main table.m-cards.m-nl td[data-label="Opens"]::before, #main table.m-cards.m-nl td[data-label="Clicks"]::before { font-size: 10px; padding: 0; }
  #main table.m-cards.m-nl td[data-label=""] { grid-column: 1 / -1; display: flex; justify-content: stretch; gap: 8px; padding-top: 2px; }
  #main table.m-cards.m-nl td[data-label=""]::before { display: none; }
  #main table.m-cards.m-nl td[data-label=""] .btn { flex: 1 1 0; min-height: 42px; }
  #main table.m-cards.m-nl td[data-label=""] .muted { display: none; }

  /* ---- Uncontacted leads look like they do on desktop: red-tinted, with a red bar down the left edge ---- */
  #main table.m-cards tr.lead-alert {
    background: rgba(255, 92, 86, .09); border-color: rgba(255, 92, 86, .4); padding-left: 18px;
    box-shadow: inset 5px 0 0 0 var(--red), var(--glass-shadow);
  }
  [data-theme="light"] #main table.m-cards tr.lead-alert { background: rgba(220, 38, 38, .06); }
  #main table.m-cards tr.lead-alert:active { background: rgba(255, 92, 86, .16); }

  /* ---- Lead cards: name, source and age, account, owner, who contacted them, then the three actions ---- */
  #main table.m-cards.m-lead tr { grid-template-columns: minmax(0, 1fr) auto; row-gap: 7px; padding-right: 14px; }
  #main table.m-cards.m-lead td.m-title { grid-column: 1 / -1; padding-bottom: 0; padding-right: 34px; }
  #main .toolbar .m-search { flex: 1 1 100%; width: 100%; }
  #main table.m-cards.m-lead td.m-badge, #main table.m-cards.m-lead td.m-badge-wide { grid-column: 1; grid-row: 2; justify-content: flex-start; padding: 0; align-self: center; }
  #main table.m-cards.m-lead td.m-badge .badge { white-space: normal; }
  #main table.m-cards.m-lead td[data-label="Added"] { grid-column: 2; grid-row: 2; justify-content: flex-end; align-self: center; padding: 0; color: var(--muted); font-size: 12px; white-space: nowrap; }
  #main table.m-cards.m-lead td[data-label="Added"]::before { display: none; }
  #main table.m-cards.m-lead td[data-label="Account"] { justify-content: flex-start; text-align: left; color: var(--muted); font-size: 14px; }
  #main table.m-cards.m-lead td[data-label="Account"]::before { display: none; }
  #main table.m-cards.m-lead td[data-label="Lead Owner"] { align-items: center; }
  #main table.m-cards.m-lead td[data-label="Lead Owner"] select { flex: 0 1 200px; min-height: 40px; }
  #main table.m-cards.m-lead td[data-label="Contacted By"] { align-items: center; }
  #main table.m-cards.m-lead tr.lead-alert td[data-label="Contacted By"] span { display: none; }
  #main table.m-cards.m-lead tr.lead-alert td[data-label="Contacted By"]::after { content: "Not contacted"; color: var(--red); font-weight: 800; font-size: 13px; }
  #main table.m-cards.m-lead td[data-label=""] { grid-column: 1 / -1; display: flex; gap: 8px; padding-top: 4px; justify-content: stretch; }
  #main table.m-cards.m-lead td[data-label=""]::before { display: none; }
  #main table.m-cards.m-lead td[data-label=""] .btn { flex: 1 1 0; min-height: 44px; }
  #main table.m-cards.m-lead td[data-label=""] .btn.primary { flex: 1.6 1 0; }
  .lead-bulk-bar { bottom: calc(68px + env(safe-area-inset-bottom)); flex-wrap: wrap; }

  /* ---- Newsletter editor ---- */
  .toolbar:has(> #send-now) { gap: 8px; }
  .toolbar:has(> #send-now) > .grow { display: none; }
  .toolbar:has(> #send-now) > .page-title { flex: 1 1 100%; }
  #save-draft, #send-test { flex: 1 1 calc(50% - 4px); order: 1; min-height: 46px; }
  #send-review { flex: 1 1 100%; order: 2; min-height: 46px; }
  #send-now { flex: 1 1 100%; order: 4; margin-top: 18px; min-height: 50px; font-size: 16px; background: transparent; color: var(--mint); border: 2px solid var(--mint); box-shadow: none; }
  #del-newsletter { flex: 1 1 100%; order: 5; min-height: 40px; background: none; border: 0; box-shadow: none; font-size: 13px; }
  .m-seg { display: flex; margin: 4px 0 14px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--card); }
  .m-seg button { flex: 1; min-height: 44px; background: none; border: 0; color: var(--muted); font: inherit; font-weight: 700; cursor: pointer; }
  .m-seg button.on { background: var(--mint); color: #fff; }
  .editor-layout.m-ed.m-show-preview > :first-child { display: none; }
  .editor-layout.m-ed:not(.m-show-preview) > :last-child { display: none; }
  .editor-layout.m-ed #preview { width: 100%; height: calc(100vh - 250px); min-height: 440px; border-radius: 12px; }
  .m-fold { display: none; }
  .m-fold.open { display: block; }
  .m-fold-btn { width: 100%; justify-content: space-between; text-align: left; margin: 2px 0 6px; }
  .block-head .drag-handle { display: none; }
  .block-actions button { min-width: 42px; min-height: 42px; font-size: 18px; }
  .rt-toolbar button, .rt-toolbar select { min-width: 40px; min-height: 40px; }
  .add-block-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .add-block-row .btn { min-height: 44px; }

  /* ---- List tables become cards: name as the title, status badge top right, details underneath ---- */
  #main .table-wrap { background: none; border: 0; box-shadow: none; overflow: visible; }
  #main table.m-cards, #main table.m-cards tbody { display: block; width: 100%; overflow: visible; background: none; border-radius: 0; }
  #main table.m-cards thead { display: none; }
  #main table.m-cards tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 12px; row-gap: 2px; position: relative;
    padding: 12px 14px; margin: 0 0 10px; background: var(--card); border: 1px solid var(--border);
    border-radius: 14px; box-shadow: var(--glass-shadow);
  }
  #main table.m-cards tr.clickable:active { background: rgba(229, 9, 20, .07); }
  /* A list inside a card (an account's contacts or deals) is flatter, so it does not look like a card within a card */
  #main .card table.m-cards tr { box-shadow: none; background: var(--bg); }
  #main table.m-cards td {
    display: flex; justify-content: space-between; gap: 12px; grid-column: 1 / -1; min-width: 0;
    padding: 2px 0; border: 0 !important; font-size: 13px; text-align: right; overflow-wrap: anywhere;
  }
  #main table.m-cards td::before {
    content: attr(data-label); flex: 0 0 auto; color: var(--muted); font-size: 11px; font-weight: 700;
    text-transform: uppercase; letter-spacing: .4px; text-align: left; padding-top: 2px;
  }
  #main table.m-cards td.m-title { grid-column: 1; align-items: center; justify-content: flex-start; text-align: left; font-size: 16px; padding-bottom: 6px; }
  #main table.m-cards td.m-title::before { display: none; }
  #main table.m-cards td.m-badge { grid-column: 2; grid-row: 1; justify-content: flex-end; align-self: start; padding: 0; }
  #main table.m-cards td.m-badge::before { display: none; }
  #main table.m-cards td.m-badge-wide { grid-column: 1 / -1; grid-row: 2; justify-content: flex-start; padding: 0 0 6px; }
  #main:has(.m-fab) { padding-bottom: calc(150px + env(safe-area-inset-bottom)); }
  #main table.m-cards td.m-check { position: absolute; top: 10px; right: 12px; width: auto; padding: 0; }
  #main table.m-cards td.m-check::before { display: none; }
  #main table.m-cards td.m-empty { display: none; }
  #main table.m-cards tr.m-has-check { padding-right: 46px; }

  /* ---- Wide content scrolls sideways inside its card instead of stretching the whole page ---- */
  #main table { display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  #main pre, #main code { white-space: pre-wrap; word-break: break-word; }
  #main img, #main video { max-width: 100%; height: auto; }
  .tabs { margin-left: -14px; margin-right: -14px; padding: 0 14px; }
}

/* Very small phones */
@media (max-width: 380px) {
  #main { padding-left: 10px; padding-right: 10px; }
  #mobile-tabs > a, #mobile-tabs > button { font-size: 10px; }
}
