/* Phones & tablets: bottom tab bar, slide-in menu, card tables, full-screen dialogs, Edit/Preview switch.
   Loaded last so it overrides the desktop layout. */
.mtabs, .nav-scrim, .mswitch, .nav-close { display: none; }

@media (max-width: 1100px) {
  :root { --tabs-h: calc(62px + env(safe-area-inset-bottom)); }
  html, body { height: auto; }
  body { padding-bottom: var(--tabs-h); overflow-x: hidden; }
  .app, .app.no-preview, .app.mode-page { display: block; height: auto; }
  .app > * { min-width: 0; }

  /* Side menu becomes a drawer */
  .nav { position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; width: min(84vw, 320px); flex-direction: column; align-items: stretch;
    padding: calc(16px + env(safe-area-inset-top)) 14px calc(16px + env(safe-area-inset-bottom)); gap: 18px; overflow-y: auto; overflow-x: hidden;
    transform: translateX(-105%); transition: transform .22s ease; box-shadow: 0 0 40px rgba(10, 6, 40, .35); }
  body.nav-open .nav { transform: none; }
  .nav-links { flex-direction: column; }
  .nav-label { display: block; }
  .nav-link { padding: 13px 12px; font-size: 15px; }
  .nav-foot { flex-direction: column; align-items: stretch; }
  .nav-user { display: block; }
  .nav-close { display: grid; place-items: center; position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 10px; width: 40px; height: 40px;
    border: 0; border-radius: 10px; background: rgba(255, 255, 255, .12); color: #fff; font-size: 18px; cursor: pointer; }
  .nav-scrim { display: block; position: fixed; inset: 0; z-index: 39; background: rgba(20, 14, 50, .45); opacity: 0; pointer-events: none; transition: opacity .2s; }
  body.nav-open .nav-scrim, body.search-open .nav-scrim { opacity: 1; pointer-events: auto; }
  body.nav-open, body.search-open { overflow: hidden; }

  /* Bottom tab bar */
  .mtabs { display: flex; position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; height: var(--tabs-h); padding-bottom: env(safe-area-inset-bottom);
    background: #fff; border-top: 1px solid var(--line); box-shadow: 0 -4px 20px rgba(20, 10, 60, .06); }
  .mtab { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; border: 0; background: none;
    color: var(--muted); font-size: 11px; font-weight: 600; cursor: pointer; text-decoration: none; -webkit-tap-highlight-color: transparent; }
  .mtab svg { width: 24px; height: 24px; fill: currentColor; }
  .mtab.on { color: var(--primary); }
  .mtab.new span.plus { width: 44px; height: 44px; margin-top: -18px; border-radius: 50%; background: var(--primary); color: #fff; display: grid; place-items: center;
    font-size: 26px; font-weight: 400; box-shadow: 0 6px 16px rgba(58, 33, 163, .4); }

  /* Search opens as a sheet at the top */
  .finder.m { display: none; position: fixed; z-index: 45; top: 0; left: 0; right: 0; margin: 0; padding: calc(12px + env(safe-area-inset-top)) 12px 12px;
    background: var(--primary); width: auto; }
  body.search-open .finder.m { display: block; }
  .finder.m input { font-size: 16px; padding: 12px; background: #fff; color: var(--ink); }
  .finder.m .btn { display: none; }
  .finder.m .finder-out { position: static; width: auto; margin-top: 10px; max-height: calc(100vh - 120px); }
  .fr { padding: 12px 10px; }

  /* Pages */
  .page { padding: calc(16px + env(safe-area-inset-top)) 14px 24px; overflow: visible; }
  .pg-head { align-items: stretch; flex-direction: column; gap: 12px; margin-bottom: 16px; }
  .pg-head h1 { font-size: 22px; }
  .pg-actions { gap: 8px; }
  .pg-actions > * { flex: 1 1 auto; }
  .pg-actions .search, .pg-actions select { flex: 1 1 100%; min-width: 0; }
  .pg-actions .seg.tabs { flex: 1 1 100%; overflow-x: auto; }
  .tiles, .tiles.small, .tiles.four { grid-template-columns: 1fr 1fr; gap: 10px; }
  .cols { grid-template-columns: 1fr; }
  .card { padding: 14px; }
  .chips { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; -webkit-overflow-scrolling: touch; }
  .chips .chipbtn { flex: none; }

  /* Touch-friendly controls; 16px text stops iPhone zooming into fields */
  .btn { padding: 10px 14px; font-size: 14px; }
  .btn.small { padding: 8px 12px; font-size: 13px; }
  .fld input[type=text], .fld input[type=number], .fld input[type=date], .fld input[type=password], .fld input[type=search],
  .fld select, .fld textarea, .search, input[type=email], .saved-select { font-size: 16px; padding: 10px 12px; }
  .seg button { padding: 10px 6px; font-size: 14px; }
  .toggle input { width: 22px; height: 22px; }
  .toast { bottom: calc(var(--tabs-h) + 12px); max-width: calc(100vw - 32px); text-align: center; }

  /* Dialogs become full-screen sheets */
  .modal { padding: 0; display: flex; align-items: stretch; z-index: 50; }
  .modal-card { max-width: none; height: 100vh; height: 100dvh; max-height: none; border-radius: 0; padding-top: env(safe-area-inset-top); }
  .modal-head { position: sticky; top: 0; background: #fff; z-index: 1; }
  .modal-head .icon-btn { width: 40px; height: 40px; font-size: 18px; }
  .modal-body { flex: 1; min-height: 0; padding: 4px 16px 20px; -webkit-overflow-scrolling: touch; }
  .modal-foot { padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); gap: 8px; }
  .modal-foot .btn { flex: 1 1 auto; }
  .modal-foot .btn.primary { order: 10; flex-basis: 100%; padding: 13px; font-size: 15px; }

  /* Studio: one panel at a time with an Edit / Preview switch */
  .app.mode-studio .editor { display: flex; }
  .app.mode-studio .preview { display: none; }
  body.show-preview .app.mode-studio .editor { display: none; }
  body.show-preview .app.mode-studio .preview { display: flex; }
  .editor-head { padding: calc(14px + env(safe-area-inset-top)) 14px 12px; }
  .doc-actions { flex-wrap: wrap; }
  .form { padding: 4px 14px 90px; overflow: visible; }
  .swatches { grid-template-columns: repeat(2, 1fr); }
  .seg { flex-wrap: wrap; }
  .preview-bar { flex-direction: column; align-items: stretch; padding: calc(12px + env(safe-area-inset-top)) 14px 12px; }
  .preview-actions { flex-wrap: wrap; }
  .preview-actions .btn { flex: 1 1 auto; }
  .preview-actions .btn.primary { flex-basis: 100%; padding: 13px; font-size: 15px; }
  .preview-actions #btnWa { order: -1; flex-basis: 100%; padding: 13px; font-size: 15px; }
  .modal-foot .btn.wa { flex-basis: 100%; padding: 13px; font-size: 15px; }
  .stage-wrap { padding: 14px 14px 90px; min-height: 0; overflow: visible; }
  body.in-studio .mswitch { display: flex; }
  .mswitch { position: fixed; z-index: 31; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabs-h) + 12px); background: var(--ink); border-radius: 999px; padding: 4px;
    box-shadow: 0 8px 24px rgba(10, 6, 40, .35); }
  .mswitch button { border: 0; background: none; color: rgba(255, 255, 255, .75); font-weight: 600; font-size: 14px; padding: 10px 22px; border-radius: 999px; cursor: pointer; }
  .mswitch button.on { background: #fff; color: var(--ink); }
}

/* Phones */
@media (max-width: 640px) {
  .grid { gap: 12px 10px; }
  .fld.w3, .fld.w4 { grid-column: span 6; }
  .fld.w2 { grid-column: span 3; }
  .tiles .tile b { font-size: 20px; }

  /* Tables turn into one card per row: first column as the title, the rest as label / value lines */
  .dt thead { display: none; }
  .dt, .dt tbody, .dt tr, .dt td { display: block; width: 100%; }
  .dt tbody tr { padding: 12px 14px; border-bottom: 1px solid var(--line); }
  .dt tbody tr:last-child { border-bottom: 0; }
  .dt td { border: 0; padding: 3px 0; display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; text-align: right; }
  .dt td::before { content: attr(data-label); flex: none; max-width: 45%; text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding-top: 1px; }
  .dt td[data-label=""]::before { display: none; }
  .dt td[data-label=""] { justify-content: flex-end; }
  .dt td:first-child { display: block; text-align: left; font-size: 15px; padding-bottom: 6px; }
  .dt td:first-child::before { display: none; }
  .dt td small { display: block; }
  .dt td > .cv { min-width: 0; overflow-wrap: anywhere; }
  .dt td:first-child > .cv { display: block; }
  .dt td.empty { text-align: center; display: block; padding: 18px 0; }
  .dt td.empty::before { display: none; }
  .tbl-wrap { overflow: visible; }
  .card.flush { overflow: visible; }

  .dept-grid, .dir-grid { grid-template-columns: 1fr; }
  .quick-grid { grid-template-columns: 1fr 1fr; }
  .card.profile { flex-direction: column; align-items: stretch; }
}
