/* =========================================================================
 *  CTWCAD — Mobile-friendly layer
 *  ──────────────────────────────
 *  Activated when <html data-mobile="1"> is set by the runtime detector.
 *  Falls back gracefully via plain @media for resize cases.
 * =======================================================================*/

/* ------- mobile-only viewport tuning ------- */
:root[data-mobile="1"] {
  --ct-mobile-bar-h: 52px;
  --ct-mobile-tabbar-h: 56px;
}

:root[data-mobile="1"] body {
  font-size: 15px;
  /* Avoid weird iOS text bumping */
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

/* Disable hover-only chrome on mobile (cleaner, no sticky highlights) */
:root[data-mobile="1"] .ct-card-actions { opacity: 1 !important; }

/* ───────────────────────── topbar ───────────────────────── */
:root[data-mobile="1"] .ct-topbar {
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  padding: 8px 12px;
  height: var(--ct-mobile-bar-h);
}
:root[data-mobile="1"] .ct-topbar-left { gap: 10px; }
:root[data-mobile="1"] .ct-topnav { display: none; } /* moved into bottom tab bar */
:root[data-mobile="1"] .ct-topbar-search {
  height: 36px; padding: 6px 10px;
  width: 100%; min-width: 0;
}
:root[data-mobile="1"] .ct-topbar-search kbd { display: none; }
:root[data-mobile="1"] .ct-topbar-search > .ct-dim { font-size: 13px; }
:root[data-mobile="1"] .ct-topbar-right > .ct-sync,
:root[data-mobile="1"] .ct-topbar-right > button[title="Settings"] {
  display: none;
}
:root[data-mobile="1"] .ct-topbar-right { gap: 4px; }

/* ───────────────────────── bottom tab bar ───────────────────────── */
.ct-mobile-tabbar {
  display: none;
}
:root[data-mobile="1"] .ct-mobile-tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 0;
  height: calc(var(--ct-mobile-tabbar-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--ct-bg-glass);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--ct-border-soft);
}
.ct-mobile-tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; padding: 6px 4px; color: var(--ct-fg-mute);
  font-size: 10.5px; letter-spacing: 0.04em;
  transition: color 0.15s;
}
.ct-mobile-tab.is-active { color: var(--ct-accent); }
.ct-mobile-tab > svg { width: 20px; height: 20px; }

/* main content needs bottom padding so tabbar doesn't cover it */
:root[data-mobile="1"] .ct-main {
  padding-bottom: calc(var(--ct-mobile-tabbar-h) + env(safe-area-inset-bottom, 0px));
}

/* ───────────────────────── pages ───────────────────────── */
:root[data-mobile="1"] .ct-page {
  padding: 18px 14px 32px;
}
:root[data-mobile="1"] .ct-page-head {
  flex-direction: column; align-items: flex-start; gap: 14px;
  margin-bottom: 22px;
}
:root[data-mobile="1"] .ct-page-head h1 {
  font-size: 22px; line-height: 1.2;
}
:root[data-mobile="1"] .ct-page-head-cta { width: 100%; }
:root[data-mobile="1"] .ct-page-head-cta .ct-btn { flex: 1; justify-content: center; }
:root[data-mobile="1"] .ct-row-split { grid-template-columns: 1fr; gap: 18px; }
:root[data-mobile="1"] .ct-section { margin-bottom: 24px; }

/* ───────────────────────── project + recent cards ───────────────────────── */
:root[data-mobile="1"] .ct-projects-grid {
  grid-template-columns: 1fr;
  gap: 12px;
}
:root[data-mobile="1"] .ct-project-cover { aspect-ratio: 16 / 7; }

/* ───────────────────────── file browser ───────────────────────── */
:root[data-mobile="1"] .ct-browser {
  grid-template-columns: 1fr;
  min-height: calc(100vh - var(--ct-mobile-bar-h));
}
:root[data-mobile="1"] .ct-browser-rail {
  display: none; /* desktop sidebar hidden; rail is its own slide-over via mobile button */
}
:root[data-mobile="1"][data-rail-open="1"] .ct-browser-rail {
  display: block;
  position: fixed; top: var(--ct-mobile-bar-h); left: 0; bottom: var(--ct-mobile-tabbar-h);
  width: min(82vw, 320px); z-index: 45;
  background: var(--ct-bg-elev); border-right: 1px solid var(--ct-border);
  box-shadow: var(--ct-shadow-3);
  padding: 12px 6px 80px;
}
:root[data-mobile="1"][data-rail-open="1"]::before {
  content: ""; position: fixed;
  top: var(--ct-mobile-bar-h); left: 0; right: 0; bottom: 0;
  background: oklch(0 0 0 / 0.45); z-index: 44;
}

:root[data-mobile="1"] .ct-browser-bar {
  flex-wrap: wrap; gap: 10px;
  padding: 10px 12px;
  top: var(--ct-mobile-bar-h);
}
:root[data-mobile="1"] .ct-crumbs {
  flex: 1 1 100%; flex-wrap: nowrap; overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
:root[data-mobile="1"] .ct-crumbs::-webkit-scrollbar { display: none; }
:root[data-mobile="1"] .ct-crumb { white-space: nowrap; flex-shrink: 0; }
:root[data-mobile="1"] .ct-bar-tools { width: 100%; gap: 6px; }
:root[data-mobile="1"] .ct-bar-tools .ct-search { display: none; }
:root[data-mobile="1"] .ct-bar-tools .ct-btn-primary { flex: 1; justify-content: center; }
:root[data-mobile="1"] .ct-toggle-group { display: none; }
/* Mobile-only "menu" button to open folder rail */
.ct-mobile-rail-btn { display: none; }
:root[data-mobile="1"] .ct-mobile-rail-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 6px;
  border: 1px solid var(--ct-border-soft); background: var(--ct-bg-elev);
  color: var(--ct-fg-dim);
}

:root[data-mobile="1"] .ct-browser-body {
  padding: 14px 12px 80px;
}
:root[data-mobile="1"] .ct-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
:root[data-mobile="1"] .ct-card-name { font-size: 11.5px; }
:root[data-mobile="1"] .ct-card-row  { font-size: 10.5px; }

/* List view becomes a single-line stack on mobile */
:root[data-mobile="1"] .ct-list-head { display: none; }
:root[data-mobile="1"] .ct-row {
  grid-template-columns: 56px 1fr auto;
  gap: 10px; padding: 10px 8px;
}
:root[data-mobile="1"] .ct-row > .ct-row-cell:nth-of-type(1),
:root[data-mobile="1"] .ct-row > .ct-row-cell:nth-of-type(2) { display: none; }

/* ───────────────────────── drawer = fullscreen sheet ───────────────────────── */
:root[data-mobile="1"] .ct-drawer {
  width: 100vw; max-width: 100vw;
  inset: 0; height: 100dvh;
  border-left: 0;
  display: flex; flex-direction: column;
}
:root[data-mobile="1"] .ct-drawer-head { padding: 10px 12px; }
:root[data-mobile="1"] .ct-drawer-title-name { font-size: 14px; }
:root[data-mobile="1"] .ct-drawer-preview { aspect-ratio: auto; }

/* The previewer in the drawer: full bleed, fills available space */
:root[data-mobile="1"] .ct-drawer-preview .ct-preview {
  height: 46vh !important;
}
:root[data-mobile="1"] .ct-tabs { padding: 0 12px; gap: 14px; }
:root[data-mobile="1"] .ct-drawer-body { padding: 14px 12px 90px; }

/* Floating fullscreen pill in mobile previewer */
.ct-preview-fs-pill { display: none; }
:root[data-mobile="1"] .ct-preview-fs-pill {
  position: absolute; right: 10px; top: 10px; z-index: 5;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 10px; border-radius: 999px;
  background: oklch(from var(--ct-bg) l c h / 0.78);
  backdrop-filter: blur(8px);
  border: 1px solid var(--ct-border-soft);
  color: var(--ct-fg);
  font-size: 11px; font-family: var(--ct-mono);
  pointer-events: auto;
}

/* Fullscreen previewer — covers everything, including tab bar */
:root[data-mobile="1"] .ct-preview.is-fullscreen {
  z-index: 9999; height: 100dvh; width: 100vw;
  inset: 0;
  background: var(--ct-bg);
}
:root[data-mobile="1"] .ct-preview.is-fullscreen .ct-preview-svg { width: 100%; height: 100%; }

/* The default close button is fine but ensure tap target on mobile */
:root[data-mobile="1"] .ct-preview-close {
  width: 44px; height: 44px;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: 12px;
}
/* Mobile fullscreen hint */
.ct-preview-fs-hint { display: none; }
:root[data-mobile="1"] .ct-preview.is-fullscreen .ct-preview-fs-hint {
  display: flex;
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 18px);
  align-items: center; gap: 8px;
  padding: 8px 14px; border-radius: 999px;
  background: oklch(from var(--ct-bg) l c h / 0.72);
  backdrop-filter: blur(8px);
  border: 1px solid var(--ct-border-soft);
  font-size: 11px; color: var(--ct-fg-dim);
  font-family: var(--ct-mono);
  pointer-events: none;
}

/* ───────────────────────── overlays ───────────────────────── */
:root[data-mobile="1"] .ct-palette {
  width: 94vw; top: 8%;
}
:root[data-mobile="1"] .ct-toasts {
  right: 14px; left: 14px;
  bottom: calc(var(--ct-mobile-tabbar-h) + env(safe-area-inset-bottom, 0px) + 12px);
}
:root[data-mobile="1"] .ct-toast { width: 100%; }
:root[data-mobile="1"] .ct-upload-manager {
  right: 12px; left: 12px; width: auto;
  bottom: calc(var(--ct-mobile-tabbar-h) + env(safe-area-inset-bottom, 0px) + 12px);
}
:root[data-mobile="1"] .ct-modal,
:root[data-mobile="1"] .ct-modal.ct-modal-rename,
:root[data-mobile="1"] .ct-modal.ct-modal-move,
:root[data-mobile="1"] .ct-modal.ct-modal-share {
  width: calc(100vw - 24px);
}

/* Hide hover-only "drag to orbit" hints */
:root[data-mobile="1"] .ct-preview-hint { display: none; }

/* Drop overlay isn't useful on mobile — hide */
:root[data-mobile="1"] .ct-drop-overlay-window { display: none !important; }

/* Settings + admin tweaks */
:root[data-mobile="1"] .ct-device-card {
  grid-template-columns: auto 1fr;
  row-gap: 8px;
}
:root[data-mobile="1"] .ct-device-card > *:nth-child(n+3) { grid-column: 1 / -1; }

/* Sign-in card stays nice on small screens */
:root[data-mobile="1"] .ct-signin-card { padding: 24px 20px; }
