:root {
  --app-height: 100dvh;
  /* Telegram 8.0+ content safe areas -> Telegram 7.7+ safe areas -> iOS/Android env -> 0px */
  --tg-safe-top: var(--tg-content-safe-area-inset-top, var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px)));
  --tg-safe-bottom: var(--tg-content-safe-area-inset-bottom, var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px)));
  --tg-safe-left: var(--tg-content-safe-area-inset-left, var(--tg-safe-area-inset-left, env(safe-area-inset-left, 0px)));
  --tg-safe-right: var(--tg-content-safe-area-inset-right, var(--tg-safe-area-inset-right, env(safe-area-inset-right, 0px)));
}

/* ── TMA: full-screen + safe areas ─────────────────────────────────────── */
html.in-tma {
  height: var(--app-height);
  overflow: hidden;
}

html.in-tma body {
  height: var(--app-height);
  overflow: hidden;
  overscroll-behavior: none;
  padding-top: var(--tg-safe-top);
  padding-left: var(--tg-safe-left);
  padding-right: var(--tg-safe-right);
}

html.in-tma #projects-ui-root,
html.in-tma .projects-app {
  height: calc(var(--app-height) - var(--tg-safe-top));
  max-height: calc(var(--app-height) - var(--tg-safe-top));
}

/* ── TMA: hide legacy desktop sidebar (used before projects-ui) ─────────── */
html.in-tma .sidebar {
  display: none !important;
}

html.in-tma .mob-menu-btn {
  display: none !important;
}

html.in-tma .step-tabs {
  display: none !important;
}

html.in-tma .main {
  margin-left: 0 !important;
  padding-bottom: calc(60px + var(--tg-safe-bottom)) !important;
}

/* Scrollable areas */
html.in-tma .cp-content,
html.in-tma .projects-list,
html.in-tma .cp-src-tbody,
html.in-tma .cp-mob-drawer-body,
html.in-tma .cp-test-messages {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* ── Bottom tab bar ─────────────────────────────────────────────────────── */
.tma-tab-bar {
  display: none;
}

html.in-tma .tma-tab-bar {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(56px + var(--tg-safe-bottom));
  background: var(--tma-bg, #ffffff);
  border-top: 1px solid var(--tma-separator, rgba(0, 0, 0, 0.10));
  z-index: 500;
  padding-bottom: var(--tg-safe-bottom);
}

.tma-tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  padding: 6px 4px;
  font-size: 10px;
  font-weight: 500;
  color: var(--tma-hint, #8e8e93);
  transition: color 0.15s;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.tma-tab-item.active {
  color: var(--tma-btn, #3e7bfa);
}

.tma-tab-item svg {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  transition: transform 0.15s;
}

.tma-tab-item.active svg {
  transform: scale(1.1);
}

/* ── Screen slide transitions ───────────────────────────────────────────── */
html.in-tma .screen {
  opacity: 0;
  transition: opacity 0.18s ease;
  pointer-events: none;
}

html.in-tma .screen.active {
  opacity: 1;
  pointer-events: auto;
}

/* ── Skeleton loading ───────────────────────────────────────────────────── */
.skeleton {
  background: linear-gradient(90deg, #f0f2f5 25%, #e4e7eb 50%, #f0f2f5 75%);
  background-size: 200% 100%;
  animation: skeleton-pulse 1.4s ease-in-out infinite;
  border-radius: 8px;
  display: block;
}

@keyframes skeleton-pulse {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ── Pull-to-refresh indicator ──────────────────────────────────────────── */
.tma-ptr {
  display: none;
  position: fixed;
  top: calc(var(--tg-safe-top) + 56px); /* below the TMA topbar */
  left: 50%;
  transform: translateX(-50%) translateY(-60px);
  background: var(--tma-bg, #ffffff);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: transform 0.25s ease, opacity 0.25s ease;
  opacity: 0;
  color: var(--tma-btn, #3e7bfa);
}

html.in-tma .tma-ptr {
  display: flex;
}

.tma-ptr.ptr-visible {
  transform: translateX(-50%) translateY(12px);
}

.tma-ptr.ptr-loading svg {
  animation: ptr-spin 0.7s linear infinite;
}

@keyframes ptr-spin {
  to { transform: rotate(360deg); }
}

/* ── Dark theme ─────────────────────────────────────────────────────────── */
html.in-tma.tma-dark {
  color-scheme: dark;
}

html.in-tma.tma-dark body {
  background: var(--tma-bg, #1c1c1e);
  color: var(--tma-text, #f2f2f7);
}

html.in-tma.tma-dark .topbar,
html.in-tma.tma-dark .screen,
html.in-tma.tma-dark .card,
html.in-tma.tma-dark .modal-box,
html.in-tma.tma-dark .modal-content {
  background: var(--tma-bg2, #2c2c2e);
  border-color: var(--tma-separator, rgba(255, 255, 255, 0.10));
}

html.in-tma.tma-dark .skeleton {
  background: linear-gradient(90deg, #2c2c2e 25%, #3a3a3c 50%, #2c2c2e 75%);
  background-size: 200% 100%;
}

html.in-tma.tma-dark .tma-ptr {
  background: var(--tma-bg2, #2c2c2e);
}

/* ── iOS-specific height fix ─────────────────────────────────────────────── */
html.miniapp-ios-fix,
html.miniapp-ios-fix body {
  height: var(--app-height);
  min-height: var(--app-height);
  max-height: var(--app-height);
  overflow: hidden;
  overscroll-behavior: none;
  position: relative;
  touch-action: manipulation;
}

html.miniapp-ios-fix body {
  padding-top: var(--tg-safe-top);
  padding-bottom: var(--tg-safe-bottom);
  padding-left: var(--tg-safe-left);
  padding-right: var(--tg-safe-right);
}

html.miniapp-ios-fix #projects-ui-root,
html.miniapp-ios-fix .projects-app {
  height: calc(var(--app-height) - var(--tg-safe-top) - var(--tg-safe-bottom));
  max-height: calc(var(--app-height) - var(--tg-safe-top) - var(--tg-safe-bottom));
}

/* ── Tap target: prevent iOS double-tap zoom ─────────────────────────────── */
@media (max-width: 980px) {
  html.in-tma input,
  html.in-tma textarea,
  html.in-tma select,
  html.in-tma button {
    font-size: 16px !important;
  }

  html.in-tma input,
  html.in-tma textarea,
  html.in-tma select {
    transform-origin: left top;
  }

  html.in-tma .projects-btn,
  html.in-tma .cp-mobile-nav-item,
  html.in-tma .cp-mob-proj-item {
    min-height: 44px;
  }
}

/* ── TMA: hide scrollbars ────────────────────────────────────────────────── */
html.in-tma ::-webkit-scrollbar {
  display: none;
}

html.in-tma * {
  scrollbar-width: none;
}

/* ── Disable text selection on nav ─────────────────────────────────────── */
html.in-tma .tma-tab-bar,
html.in-tma .tma-layout-topbar,
html.in-tma .cp-mob-drawer-header,
html.in-tma .cp-mob-drawer-handle-row {
  user-select: none;
  -webkit-user-select: none;
}

/* ══════════════════════════════════════════════════════════════════════════
   TMA MOBILE-FIRST LAYOUT
   The sidebar is physically REMOVED from the DOM by miniapp.js (setupTmaLayout).
   These styles define the new mobile topbar and cp-main layout.
   ══════════════════════════════════════════════════════════════════════════ */

/* ── projects-app: flex column (topbar + content stack) ─────────────────── */
html.in-tma .projects-app {
  flex-direction: column !important;
  overflow: hidden;
}

/* ── TMA Mobile Topbar ───────────────────────────────────────────────────── */
/* Injected by miniapp.js setupTmaLayout() as #tma-layout-topbar */
.tma-layout-topbar {
  display: none; /* only shown in TMA */
}

html.in-tma .tma-layout-topbar {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  padding-top: calc(10px + var(--tg-safe-top, 0px));
  background: var(--tma-bg, #ffffff);
  border-bottom: 1px solid var(--tma-separator, rgba(0, 0, 0, 0.08));
  flex-shrink: 0;
  min-height: 52px;
  z-index: 100;
  gap: 0;
}

/* ── Project switcher button in topbar ──────────────────────────────────── */
.tma-proj-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 8px 4px 0;
  border-radius: 10px;
  -webkit-tap-highlight-color: transparent;
  font-family: inherit;
  text-align: left;
  transition: background 0.15s;
  flex: 1;
  min-width: 0;
}

.tma-proj-btn:active {
  background: rgba(0, 0, 0, 0.05);
}

/* ── Project avatar circle ───────────────────────────────────────────────── */
.tma-proj-av {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #6366f1;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
  line-height: 1;
}

/* ── Project info column ─────────────────────────────────────────────────── */
.tma-proj-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* ── Project name — this IS #projects-active-title moved into topbar ─────── */
.tma-proj-name {
  font-size: 15px !important;
  font-weight: 600 !important;
  color: #000 !important;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}

/* ── Status row ──────────────────────────────────────────────────────────── */
.tma-proj-status-row {
  display: flex;
  align-items: center;
  gap: 5px;
}

/* Override dark-sidebar colors for status dot and label moved to topbar */
html.in-tma .tma-proj-status-row .cp-status-dot {
  width: 7px !important;
  height: 7px !important;
  flex-shrink: 0;
}

html.in-tma .tma-proj-status-row .cp-status-label {
  font-size: 12px !important;
  color: #8e8e93 !important; /* override white sidebar color */
  line-height: 1;
}

/* ── Chevron icon ─────────────────────────────────────────────────────────── */
.tma-proj-chev {
  color: #8e8e93;
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

/* ── cp-main: full width, scrollable, room for tab bar ──────────────────── */
html.in-tma .cp-main {
  margin-left: 0 !important;
  flex: 1 !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  /* Extra bottom padding for the fixed tab bar */
  padding-bottom: calc(60px + var(--tg-safe-bottom)) !important;
  /* Height = viewport minus topbar (~56px) */
  min-height: 0;
}

/* ── Hide the built-in desktop cp-topbar (inside cp-main) ───────────────── */
/* Already hidden via inline style by setupTmaLayout(), but belt-and-suspenders */
html.in-tma .cp-topbar {
  display: none !important;
}

/* ── Hide the built-in projects-ui mobile bottom nav (we use tma-tab-bar) ─ */
html.in-tma .cp-mobile-nav {
  display: none !important;
}

/* ── Project switcher drawer: above everything ───────────────────────────── */
html.in-tma .cp-mobile-drawer-bg {
  z-index: 9999 !important;
}

/* ── Dark mode: TMA topbar ───────────────────────────────────────────────── */
html.in-tma.tma-dark .tma-layout-topbar {
  background: var(--tma-bg, #1c1c1e) !important;
  border-bottom-color: var(--tma-separator, rgba(255, 255, 255, 0.1)) !important;
}

html.in-tma.tma-dark .tma-proj-name {
  color: var(--tma-text, #f2f2f7) !important;
}

html.in-tma.tma-dark .tma-proj-chev {
  color: rgba(255, 255, 255, 0.45) !important;
}

html.in-tma.tma-dark .tma-proj-btn:active {
  background: rgba(255, 255, 255, 0.07) !important;
}

html.in-tma.tma-dark .tma-proj-status-row .cp-status-label {
  color: rgba(255, 255, 255, 0.55) !important;
}

html.in-tma.tma-dark .cp-main {
  background: var(--tma-bg, #1c1c1e) !important;
}

/* ── Dark mode: tab bar ──────────────────────────────────────────────────── */
html.in-tma.tma-dark .tma-tab-bar {
  background: var(--tma-bg, #1c1c1e) !important;
  border-top-color: var(--tma-separator, rgba(255, 255, 255, 0.1)) !important;
}

html.in-tma.tma-dark .tma-tab-item {
  color: var(--tma-hint, #8e8e93) !important;
}

html.in-tma.tma-dark .tma-tab-item.active {
  color: var(--tma-btn, #3e7bfa) !important;
}

/* ── TMA content padding adjustments ─────────────────────────────────────── */
html.in-tma .cp-analytics-grid,
html.in-tma .cp-fin-kpi-row {
  padding-bottom: 8px;
}

/* ── Topbar meta text hidden in TMA ─────────────────────────────────────── */
html.in-tma .cp-topbar-meta {
  display: none !important;
}
