/* ===== LAYOUT FIX OVERRIDES v3 ===== */

/* === 1. HEADER — clean, no double padding === */
.header {
  height: auto;
  min-height: 0;
  padding: 0 16px;
  padding-top: max(var(--safe-t), env(safe-area-inset-top, 0px));
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 30;
}
.header::after {
  content: '';
  display: block;
  min-height: 52px;
  width: 0;
  flex-shrink: 0;
}
.header-logo { flex-shrink: 0; }
.header-brand {
  font-size: 16px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}
.user-avatar { flex-shrink: 0; margin-left: auto; }

/* === 2. TAB BAR — locked bottom === */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 40;
  background: rgba(14, 16, 22, 0.95);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 6px 0;
  padding-bottom: max(env(safe-area-inset-bottom, 0px), 6px);
  flex-shrink: 0;
  height: auto;
  min-height: 56px;
}
.tab-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 4px 0;
  min-width: 0;
  flex: 1;
  max-width: 80px;
}
.tab-btn-label {
  font-size: 10px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* === 3. CONTENT — enough bottom padding for tab bar + FAB === */
.content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  padding-bottom: calc(16px + 60px + max(env(safe-area-inset-bottom, 0px), 12px) + 60px);
  max-width: 100%;
  box-sizing: border-box;
}

/* === 4. AI FAB — move to right side, above tab bar, no overlap === */
.ai-fab {
  bottom: calc(56px + max(env(safe-area-inset-bottom, 0px), 12px) + 16px) !important;
  left: auto !important;
  right: 16px !important;
  z-index: 36;
}
.scroll-top-btn {
  bottom: calc(56px + max(env(safe-area-inset-bottom, 0px), 12px) + 72px) !important;
  right: 16px;
}

/* === 5. ADMIN TABS — horizontal scroll, never truncate === */
.admin-tabs {
  display: flex !important;
  flex-wrap: nowrap !important;
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  gap: 4px !important;
  padding-bottom: 4px;
  margin-bottom: 16px;
}
.admin-tabs::-webkit-scrollbar { display: none; }
.admin-tab {
  flex-shrink: 0 !important;
  white-space: nowrap !important;
  padding: 8px 16px !important;
  font-size: 13px !important;
  min-width: auto !important;
}

/* === 6. CATEGORY/FILTER PILLS — always scroll, never squish === */
.demo-cat-pills,
.year-tabs,
.tier-tabs {
  display: flex !important;
  gap: 6px;
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch;
  flex-wrap: nowrap !important;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.demo-cat-pills::-webkit-scrollbar,
.year-tabs::-webkit-scrollbar,
.tier-tabs::-webkit-scrollbar { display: none; }
.demo-cat-pill,
.year-pill,
.tier-pill {
  flex-shrink: 0 !important;
  white-space: nowrap !important;
}

/* Workout filter buttons — scroll row */
[style*="display:flex"][style*="flex-wrap:wrap"]:has(> .wo-filter-btn) {
  flex-wrap: nowrap !important;
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.wo-filter-btn {
  flex-shrink: 0 !important;
  white-space: nowrap !important;
}

/* === 7. AI COACH OVERLAY — fix blank screen === */
.ai-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding-top: max(env(safe-area-inset-top, 0px), 20px);
}
.ai-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}
.ai-input-bar {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  padding-bottom: calc(12px + max(env(safe-area-inset-bottom, 0px), 12px));
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}
.ai-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
}
.ai-msg.ai {
  align-self: flex-start;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--fg);
}

/* === 8. FITNESS SUB-TABS — locked sticky === */
.fitness-sub-bar-sticky {
  position: sticky;
  top: 0;
  z-index: 10;
  margin: -16px -16px 12px -16px;
  padding: 12px 16px 8px 16px;
  background: var(--bg);
}
.fitness-sub-tab,
.lb-sub-tab {
  flex: 1;
  padding: 10px 4px;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* === 9. TEXT OVERFLOW FIXES === */
.card-title, .wo-name, .race-name, .page-title, .section-title,
.coach-card-name, .member-name, .feed-name, .strava-activity-name,
.challenge-team-name, .demo-ex-name, .admin-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}
.card-desc, .wo-notes, .cl-desc, .pw-desc, .race-notes,
.announce-banner-msg, .category-desc, .tier-desc, .plan-rec-desc {
  white-space: normal;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

/* === 10. CARDS — prevent blowout === */
.card, .wo-card, .race-card, .strava-card, .plan-card,
.coach-card, .member-card, .demo-ex-card, .announce-banner,
.challenge-card, .today-stats-row, .today-race-row {
  overflow: hidden;
  word-break: break-word;
  max-width: 100%;
  box-sizing: border-box;
}

/* === 11. DEMO EXERCISE CARDS — badge doesn't overlap name === */
.demo-ex-header {
  display: flex;
  align-items: center;
  gap: 8px;
}
.demo-ex-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}
.demo-ex-badge {
  flex-shrink: 0 !important;
  white-space: nowrap !important;
}

/* === 12. PROFILE ROW — no overlap === */
.profile-row {
  display: flex;
  align-items: center;
  gap: 12px;
  overflow: hidden;
}
.profile-row-label { flex-shrink: 0; max-width: 45%; }
.profile-row-value {
  flex: 1; text-align: right; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; min-width: 0;
}

/* === 13. CHECKLIST ITEMS === */
.checklist-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px;
  overflow: hidden;
}
.cl-info { flex: 1; min-width: 0; overflow: hidden; }
.cl-title { word-break: break-word; }
.cl-meta { display: flex; flex-wrap: wrap; gap: 6px; overflow: hidden; }

/* === 14. WORKOUT CARDS === */
.wo-top { display: flex; align-items: flex-start; gap: 8px; overflow: hidden; }
.wo-left { flex: 1; min-width: 0; overflow: hidden; }
.wo-title-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }

/* === 15. BADGE — never wrap === */
.badge, .activity-badge { flex-shrink: 0; white-space: nowrap; }

/* === 16. GRID CHILDREN === */
.today-stats-row > *, .kpi-grid > *, .countdown-grid > *,
.tracker-stats > *, .form-row > *, .pb-grid > *,
.race-log-grid > *, .member-stats > * {
  min-width: 0;
  overflow: hidden;
}

/* === 17. SHEET === */
.sheet {
  padding-bottom: calc(20px + max(env(safe-area-inset-bottom, 0px), 12px));
}

/* === 18. OVERLAYS === */
.profile-overlay, .timer-overlay {
  padding-top: max(env(safe-area-inset-top, 0px), 20px);
}

/* === 19. USER MENU === */
.user-menu {
  position: absolute;
  top: calc(52px + max(env(safe-area-inset-top, 0px), 0px) + 4px);
  right: 16px;
}

/* === 20. RACE FOOTAGE LINKS — ensure they're tappable === */
.race-footage-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-radius: 8px;
  background: rgba(191, 255, 0, 0.06);
  border: 1px solid rgba(191, 255, 0, 0.15);
  text-decoration: none;
  color: var(--fg);
  font-size: 13px;
  margin-bottom: 8px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  min-height: 48px;
}
.race-footage-link:active {
  background: rgba(191, 255, 0, 0.15);
}
.race-footage-link svg {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
}

/* === 21. RACE VIDEO CARDS — tappable === */
.race-video-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-radius: 10px;
  background: rgba(191, 255, 0, 0.04);
  border: 1px solid rgba(191, 255, 0, 0.12);
  margin-bottom: 8px;
  text-decoration: none;
  color: var(--fg);
  cursor: pointer;
  min-height: 48px;
}

/* === 22. ADMIN PANEL — ensure content scrolls === */
.admin-section {
  overflow-y: visible;
  overflow-x: hidden;
}
.admin-section.active {
  display: block;
}

/* Admin form inputs — full width === */
.admin-form .input {
  width: 100% !important;
  box-sizing: border-box !important;
}

/* Admin exercise/plan cards — no overflow === */
.admin-exercise-card,
.perm-admin-card,
.footage-admin-item {
  overflow: hidden;
  word-break: break-word;
}

/* === 23. WEEKLY CHART — fix bar labels === */
.chart-row {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 80px;
}
.chart-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 0;
}
.chart-bar-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.chart-bar-val {
  font-size: 9px;
  font-weight: 600;
  color: var(--fg);
  min-height: 12px;
}
.chart-bar {
  width: 80%;
  border-radius: 3px 3px 0 0;
  min-height: 2px;
}
.chart-bar.current {
  background: var(--primary) !important;
}
.chart-label {
  font-size: 9px;
  color: var(--muted-fg);
  white-space: nowrap;
}

/* === 24. RESPONSIVE — small screens === */
@media (max-width: 374px) {
  .tab-btn-label { font-size: 8px; }
  .tab-btn { max-width: 60px; }
  .header-brand { font-size: 14px; }
  .fitness-sub-tab, .lb-sub-tab { font-size: 11px; padding: 8px 0; }
  .demo-cat-pill { font-size: 11px; padding: 5px 10px; }
  .admin-tab { font-size: 11px !important; padding: 6px 12px !important; }
}

/* === 25. iOS WEBVIEW === */
@supports (-webkit-touch-callout: none) {
  html, body {
    position: fixed;
    inset: 0;
    overflow: hidden;
    overscroll-behavior: none;
  }
  input, select, textarea {
    font-size: 16px !important;
  }
  .content {
    overscroll-behavior-y: contain;
  }
}
