/* =====================================================================================
   Mobile + tablet layer (loaded after app.css). Phone-first, then iPad, with Safari / iOS fixes.
   Breakpoints: phone ≤ 720px · tablet portrait 721–1024px · tablet landscape / small laptop 1025–1280px.
   ===================================================================================== */

/* ---- Base: iOS / Safari behaviour ---- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
body { -webkit-font-smoothing: antialiased; overscroll-behavior-y: none; touch-action: manipulation; }  /* no double-tap zoom, no rubber-band pull of the whole page */
img, video, canvas, svg, iframe { max-width: 100%; }
.view, .card, .card-b, .main { min-width: 0; }
.view { overflow-wrap: anywhere; }
table { -webkit-overflow-scrolling: touch; }
.table-wrap, .card-b, .tabs, .snav, .bars { -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
button, .btn, a, select, input[type=checkbox], input[type=radio], label { touch-action: manipulation; }
input, select, textarea, button { font-family: inherit; }
input[type=search], input[type=text], input[type=email], input[type=tel], input[type=number], input[type=password], input[type=date], input[type=time], input[type=month], select, textarea { -webkit-appearance: none; appearance: none; border-radius: 10px; }
select { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%2364748b' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px; }
input[type=date], input[type=time], input[type=month] { min-height: 42px; }

/* Full-height layouts use the dynamic viewport so Safari's toolbar never hides the bottom of the screen */
.app { min-height: 100vh; min-height: 100dvh; }
.side { height: 100vh; height: 100dvh; }

/* Safe areas (notch, Dynamic Island, home indicator) */
.top { padding-top: env(safe-area-inset-top); }
.side { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); padding-left: env(safe-area-inset-left); }
.view { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
.overlay { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
.toast-host, #toasts { bottom: max(16px, env(safe-area-inset-bottom)); }

/* ---- Touch devices (phones and iPads): comfortable targets, no hover-only UI ---- */
@media (pointer: coarse) {
  .btn { min-height: 42px; padding-top: 8px; padding-bottom: 8px; }
  .btn.xs { min-height: 36px; padding-left: 12px; padding-right: 12px; }
  .btn.sm { min-height: 40px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .nav a, .side-bottom a, .side-bottom button, .snav a { min-height: 46px; }
  .tabs button, .tabs a, .linktabs a { min-height: 42px; display: inline-flex; align-items: center; }
  .tbl td, .tbl th { padding-top: 12px; padding-bottom: 12px; }
  .tbl tr.click, tr[data-deal], tr[data-lead] { cursor: pointer; }
  input[type=checkbox], input[type=radio] { width: 22px; height: 22px; }
  .check, label.check { min-height: 40px; align-items: center; }
  .cd-row, .cp-row, .notif { min-height: 56px; }
}
/* iOS zooms into inputs smaller than 16px: keep every field at 16px on touch screens */
@media (pointer: coarse), (max-width: 1024px) {
  input, select, textarea { font-size: 16px; }
}
@media (hover: none) {
  .row-actions, .hover-only { opacity: 1 !important; visibility: visible !important; }
}

/* ---- Tablet (iPad portrait and landscape) ---- */
@media (min-width: 721px) and (max-width: 1024px) {
  .view { padding: 18px 20px; max-width: none; }
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fgrid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .modal { max-width: 640px; } .modal.wide { max-width: 820px; }
  .drawer { max-width: 620px; } .drawer.wide { max-width: 760px; }
  .top h1 { font-size: 20px; }
  .cp { grid-template-columns: 300px 1fr; height: calc(100dvh - 110px); }
}
@media (min-width: 901px) and (max-width: 1180px) {          /* iPad landscape / 11-12.9" */
  .app { grid-template-columns: 240px minmax(0, 1fr); }
  .app.collapsed { grid-template-columns: 76px minmax(0, 1fr); }
  .view { padding: 20px 22px; }
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .settings-wrap { grid-template-columns: 220px minmax(0, 1fr); }
}
@media (min-width: 721px) and (max-width: 900px) {            /* iPad portrait: sidebar becomes a slide-over, content is full width */
  .side { width: 300px; }
  .burger { display: inline-grid; }
}

/* ---- Phone ---- */
@media (max-width: 720px) {
  body { font-size: 15px; }
  .view { padding: 12px 12px calc(86px + env(safe-area-inset-bottom)); }        /* room for the bottom bar */
  .top { position: sticky; top: 0; z-index: 60; background: var(--panel); border-bottom: 1px solid var(--line); min-height: calc(56px + env(safe-area-inset-top)); }
  .top h1 { font-size: 17px; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .top .icon-btn { min-width: 40px; }
  .page-h { gap: 8px; } .page-h h1 { font-size: 20px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 12px; } .stat b, .stat .v { font-size: 20px; }
  .card-h { padding: 12px; } .card-b { padding: 12px; }
  .fgrid, .fgrid3 { grid-template-columns: 1fr; }
  .btn.primary, .btn.sm.primary { white-space: nowrap; }
  .row.end { flex-wrap: wrap; justify-content: flex-start; }

  /* Dialogs become bottom sheets */
  .overlay { align-items: flex-end; padding: 0; }
  .modal, .modal.wide { max-width: none; width: 100%; max-height: 92vh; max-height: 92dvh; border-radius: 18px 18px 0 0; animation: gx-sheet .22s ease-out; }
  .modal-b { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 16px; }
  .modal-h { padding: 14px 16px; position: relative; }
  .modal-h::before { content: ''; position: absolute; left: 50%; top: 6px; width: 38px; height: 4px; margin-left: -19px; border-radius: 4px; background: var(--line); }
  .modal-f { padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); position: sticky; bottom: 0; background: var(--panel); border-top: 1px solid var(--line2); }
  .modal-f .btn { flex: 1; justify-content: center; }
  .drawer, .drawer.wide { max-width: none; width: 100%; }
  .drawer-b { padding: 14px 12px calc(16px + env(safe-area-inset-bottom)); }
  .pop, .notif-pop, .umenu .pop { position: fixed !important; left: 8px !important; right: 8px !important; top: calc(60px + env(safe-area-inset-top)) !important; width: auto !important; max-height: 70dvh; overflow-y: auto; }

  /* Tables turn into readable cards */
  .table-wrap { overflow: visible; }
  table.tbl:not(.att):not(.keep) { display: block; }
  table.tbl:not(.att):not(.keep) thead { display: none; }
  table.tbl:not(.att):not(.keep) tbody { display: block; }
  table.tbl:not(.att):not(.keep) tr { display: block; padding: 10px 12px; border-bottom: 1px solid var(--line2); }
  table.tbl:not(.att):not(.keep) td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 4px 0; border: 0; text-align: right; }
  table.tbl:not(.att):not(.keep) td::before { content: attr(data-label); flex: none; color: var(--muted); font-size: 12px; text-align: left; max-width: 42%; }
  table.tbl:not(.att):not(.keep) td:empty { display: none; }
  table.tbl:not(.att):not(.keep) td[data-label=""]::before { display: none; }
  table.tbl:not(.att):not(.keep) td[data-label=""] { justify-content: flex-end; }
  table.tbl:not(.att):not(.keep) td.num { text-align: right; }
  .table-wrap:has(table.att) { overflow-x: auto; }

  /* Chat: popups are replaced by the full-screen chat; full chat uses the whole viewport */
  #chatdock .cd-wins { display: none; }
  .cp { height: calc(100dvh - 56px - 64px - env(safe-area-inset-bottom) - env(safe-area-inset-top)); border-radius: 0; border-left: 0; border-right: 0; margin: -12px -12px 0; }
  .cp-msgs { padding: 10px; } .cp-m { max-width: 86%; }
  .cp-in { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .cp-lb .cp-lbb { padding: 0 8px 12px; } .cp-nav { width: 40px; height: 40px; }

  /* Settings side menu becomes a horizontal scroller */
  .snav { display: flex; gap: 6px; overflow-x: auto; padding: 8px; white-space: nowrap; scrollbar-width: none; }
  .snav::-webkit-scrollbar { display: none; }
  .snav h2 { display: none; } .snav .sec { display: none; }
  .snav a { flex: none; border-radius: 20px; padding: 8px 14px; background: var(--bg2, rgba(0,0,0,.04)); }
  .snav a.on { background: var(--accent-bg); }
  .tabs, .linktabs { overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; } .tabs::-webkit-scrollbar, .linktabs::-webkit-scrollbar { display: none; }
  .filters select, .filters input { flex: 1 1 140px; min-width: 0; }
  .kv { grid-template-columns: 100px 1fr; }
}
@keyframes gx-sheet { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---- Bottom tab bar (phones only; built from the menu by js) ---- */
.tabbar { display: none; }
@media (max-width: 720px) {
  .tabbar { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; position: fixed; left: 0; right: 0; bottom: 0; z-index: 70; background: var(--panel); border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); box-shadow: 0 -6px 20px rgba(16, 24, 40, .08); }
  .tabbar a, .tabbar button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 52px; padding: 4px 2px; border: 0; background: none; color: var(--muted); font-size: 11px; font-weight: 500; text-decoration: none; position: relative; border-radius: 12px; cursor: pointer; }
  .tabbar .ic { width: 22px; height: 22px; }
  .tabbar .on { color: var(--primary); }
  .tabbar .on::before { content: ''; position: absolute; top: -7px; left: 30%; right: 30%; height: 3px; border-radius: 0 0 3px 3px; background: var(--primary); }
  .tabbar .tb-badge { position: absolute; top: 2px; left: 54%; min-width: 16px; height: 16px; border-radius: 8px; background: #dc2626; color: #fff; font-size: 10px; line-height: 16px; text-align: center; padding: 0 4px; }
  #chatdock .cd-bar { display: none; }
  .toast-host, #toasts { bottom: calc(76px + env(safe-area-inset-bottom)); }
}
/* landscape phones: keep the bar slim */
@media (max-width: 920px) and (max-height: 480px) {
  .tabbar a, .tabbar button { min-height: 40px; flex-direction: row; justify-content: center; gap: 6px; }
  .view { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
}
/* Installed to the home screen (standalone): no browser chrome, so respect the status bar */
@media (display-mode: standalone) {
  .top { padding-top: max(env(safe-area-inset-top), 8px); }
  body { user-select: none; -webkit-user-select: none; } input, textarea, [contenteditable], .cp-t, .cell-main, .kv dd { user-select: text; -webkit-user-select: text; }
}

/* Phone top bar: title gets the room; secondary buttons live elsewhere (chat in the bottom bar, refresh by pull / page reload) */
@media (max-width: 720px) {
  .top { gap: 6px; }
  .top h1 { flex: 1 1 auto; min-width: 0; max-width: none; }
  .top .sp { display: none; }
  .top [data-refresh], .top .chat-top { display: none; }
  .top .create-btn { padding: 0 12px; }
  .top .create-btn .ic:last-child { display: none; }
}

/* Card rows: the first cell is the card title */
@media (max-width: 720px) {
  table.tbl:not(.att):not(.keep) td:first-child { display: block; text-align: left; font-weight: 600; padding-bottom: 6px; }
  table.tbl:not(.att):not(.keep) td:first-child::before { display: none; }
}

/* iPad portrait: the settings menu becomes a chip row too, so content starts at the top */
@media (max-width: 900px) {
  .snav { display: flex; flex-wrap: nowrap; gap: 6px; overflow-x: auto; padding: 8px; white-space: nowrap; scrollbar-width: none; position: static; }
  .snav::-webkit-scrollbar { display: none; }
  .snav h2, .snav .sec { display: none; }
  .snav a { flex: none; border-radius: 20px; padding: 8px 14px; background: var(--bg2, rgba(0,0,0,.04)); }
  .snav a.on { background: var(--accent-bg); }
}

/* Get the app page */
.apps-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.apps-grid .card.recommended { border-color: var(--primary); box-shadow: 0 0 0 2px var(--accent-bg); }
.apps-grid .card-h h3 { display: flex; align-items: center; gap: 8px; }
.qr { display: inline-block; text-align: center; margin: 12px 0 4px; }
.qr-img { width: 168px; height: 168px; padding: 8px; background: #fff; border: 1px solid var(--line); border-radius: 12px; }
.qr-img svg { width: 100%; height: 100%; display: block; }
.qr small { display: block; max-width: 190px; margin: 6px auto 0; color: var(--muted); }
.steps { padding-left: 20px; margin: 6px 0 10px; } .steps li { margin: 4px 0; }

/* ---- Compact sidebar footer: icons only, in the same row as Collapse ---- */
.side-bottom { padding: 6px 10px 8px; display: block; }
.sb-row { display: flex; align-items: center; gap: 4px; }
.sb-row .sb-sp { flex: 1; }
.side-bottom .sb-row a, .side-bottom .sb-row button { width: 40px; height: 40px; min-height: 40px; padding: 0; gap: 0; display: grid; place-items: center; border-radius: 10px; flex: none; }
.side-bottom .sb-row a.on { box-shadow: none; }
.side-bottom .sb-row .ic { width: 20px; height: 20px; }
.app.collapsed .sb-row { flex-direction: column; gap: 2px; } .app.collapsed .sb-row .sb-sp { display: none; }
.app.collapsed .side-bottom .sb-row a, .app.collapsed .side-bottom .sb-row button { padding: 0; }
@media (max-width: 900px) { .sb-row { justify-content: space-around; } .sb-row .sb-sp { display: none; } .side-bottom .sb-row a { width: 48px; height: 46px; } }

/* ---- Smarter menu: folding sections, slim scrollbar ---- */
.nav-sec.foldable { cursor: pointer; display: flex; align-items: center; justify-content: space-between; user-select: none; -webkit-user-select: none; border-radius: 8px; }
.nav-sec.foldable:hover { color: var(--side-t); }
.nav-sec.foldable::after { content: ''; width: 7px; height: 7px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); opacity: .55; transition: transform .15s; margin-right: 6px; flex: none; }
.nav-sec.foldable.is-folded::after { transform: rotate(-45deg); }
.nav-sec.foldable.is-folded { padding-bottom: 8px; }
.app:not(.collapsed) #nav:not(.searching) .nf-hide { display: none !important; }
.side-scroll { scrollbar-width: thin; scrollbar-color: transparent transparent; overscroll-behavior: contain; scroll-behavior: auto; }
.side-scroll:hover, .side-scroll:focus-within { scrollbar-color: rgba(128, 128, 128, .45) transparent; }
.side-scroll::-webkit-scrollbar { width: 6px; } .side-scroll::-webkit-scrollbar-thumb { background: transparent; border-radius: 6px; }
.side-scroll:hover::-webkit-scrollbar-thumb { background: rgba(128, 128, 128, .45); }
@media (max-width: 900px) { .app:not(.nav-open) .side-scroll { scrollbar-color: transparent transparent; } }

/* Users table: the Clients column stays on one tidy line, however many clients a person has */
td.clients-col, th.clients-col { white-space: nowrap; min-width: 190px; }
.cchips { display: flex; align-items: center; gap: 4px; flex-wrap: nowrap; }
.cchip { display: inline-block; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: middle; padding: 3px 9px; border-radius: 12px; font-size: 12px; font-weight: 500; background: var(--accent-bg); color: var(--text-strong, inherit); border: 1px solid transparent; text-decoration: none; }
button.cchip.more { cursor: pointer; background: var(--primary); color: #fff; max-width: none; font-weight: 600; }
.cchips.all { flex-wrap: wrap; margin-bottom: 10px; } .cchips.all .cchip { max-width: 100%; }
a.cchip:hover { border-color: var(--primary); }
@media (max-width: 720px) { td.clients-col { min-width: 0; white-space: normal; } .cchips { flex-wrap: wrap; justify-content: flex-end; } }

/* Leads: pick several sources together (mobile first: a bottom sheet on phones, a floating list on wider screens) */
.msel { position: relative; display: inline-block; }
.msel > summary { list-style: none; cursor: pointer; padding: 0 34px 0 14px; border: 1px solid var(--line, rgba(128,128,128,.35)); border-radius: 12px; background: var(--card, transparent); white-space: nowrap; min-height: 44px; display: flex; align-items: center; position: relative; font: inherit; }
.msel > summary::-webkit-details-marker { display: none; }
.msel > summary::after { content: ''; position: absolute; right: 14px; top: 50%; width: 7px; height: 7px; margin-top: -6px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); opacity: .6; }
.msel[open] > summary::after { transform: rotate(-135deg); margin-top: -2px; }
.msel.on > summary { border-color: var(--primary); }
.msel-box { display: none; }
.msel-box.open { display: block; position: fixed; z-index: 1001; left: 0; right: 0; bottom: 0; max-height: 65vh; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 12px calc(14px + env(safe-area-inset-bottom)); border-radius: 18px 18px 0 0; background: var(--card-solid, var(--bg, #fff)); border: 1px solid var(--line, rgba(128,128,128,.35)); box-shadow: 0 -12px 40px rgba(0,0,0,.35); }
.msel-veil { position: fixed; inset: 0; z-index: 1000; background: rgba(0,0,0,.35); }
.msel-box .msel-done { display: block; width: 100%; margin-top: 8px; min-height: 46px; }
.msel-box label.check { display: flex !important; flex-direction: row !important; align-items: center; justify-content: flex-start !important; gap: 14px; width: 100%; min-height: 48px; padding: 6px 8px; margin: 0; border-radius: 10px; text-align: left; cursor: pointer; }
.msel-box label.check:hover { background: rgba(128,128,128,.14); }
.msel-box label.check input[type=checkbox] { width: 22px !important; height: 22px !important; min-height: 0 !important; min-width: 22px; padding: 0 !important; margin: 0; flex: none; accent-color: var(--primary, #f97316); }
.msel-box label.check span { flex: 1; text-align: left; }
@media (min-width: 721px) {
  .msel-box.open { left: var(--mx, 8px); right: auto; bottom: auto; top: var(--my, 60px); width: 300px; max-height: 60vh; border-radius: 14px; box-shadow: 0 14px 34px rgba(0,0,0,.3); }
  .msel-veil { background: transparent; }
  .msel-box .msel-done { display: none; }
}
}
