/**
 * Dynamic Viewport Height Fixes
 * Handles iOS Safari address bar, keyboard appearance, etc.
 * 
 * Problem: On mobile browsers, 100vh includes the address bar area,
 * causing content to be hidden behind browser chrome.
 * 
 * Solutions:
 * 1. dvh (dynamic viewport height) - modern browsers
 * 2. --vh CSS variable set by JS - universal fallback
 * 3. -webkit-fill-available - iOS Safari specific
 */

/* ============================================
   FULL HEIGHT UTILITIES
   ============================================ */

/* Standard full height - may have issues on mobile */
.full-height {
  height: 100vh;
}

/* Dynamic full height - best for mobile */
.full-height-dynamic {
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height - adjusts with browser chrome */
}

/* JS-controlled full height (most reliable cross-browser) */
.full-height-js {
  height: calc(var(--vh, 1vh) * 100);
}

/* iOS Safari specific full height */
.full-height-ios {
  height: 100vh;
  height: -webkit-fill-available;
}

/* Safe full height - accounts for safe areas */
.full-height-safe {
  height: calc(100vh - var(--safe-area-top, 0px) - var(--safe-area-bottom, 0px));
  height: calc(100dvh - var(--safe-area-top, 0px) - var(--safe-area-bottom, 0px));
}

/* ============================================
   MIN/MAX HEIGHT UTILITIES
   ============================================ */

.min-full-height {
  min-height: 100vh;
  min-height: 100dvh;
}

.max-full-height {
  max-height: 100vh;
  max-height: 100dvh;
}

/* ============================================
   BODY & HTML VIEWPORT FIXES
   ============================================ */

html {
  /* Prevent iOS bounce scrolling showing background */
  height: 100%;
  overflow: hidden;
}

body {
  /* Use dynamic viewport height where supported */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* iOS Safari specific body fix */
body.is-ios {
  height: 100%;
  height: -webkit-fill-available;
}

/* PWA mode - use full available height */
body.is-pwa {
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ============================================
   SHORT VIEWPORT HANDLING
   When height < 600px (keyboard visible, small device)
   ============================================ */

body.is-short-viewport {
  /* Reduce spacing to fit more content */
  --spacing-scale: 0.75;
}

/* Reduce padding in short viewports */
body.is-short-viewport .p-4 {
  padding: 0.75rem;
}

body.is-short-viewport .p-6 {
  padding: 1rem;
}

body.is-short-viewport .py-4 {
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}

body.is-short-viewport .py-6 {
  padding-top: 1rem;
  padding-bottom: 1rem;
}

/* Reduce gaps in short viewports */
body.is-short-viewport .gap-4 {
  gap: 0.5rem;
}

body.is-short-viewport .gap-6 {
  gap: 0.75rem;
}

body.is-short-viewport .space-y-4 > * + * {
  margin-top: 0.5rem;
}

/* ============================================
   RESPONSIVE ACTIVE CALL VIEW (MOBILE)
   ============================================ */

/* Base mobile active call - fluid sizing */
body.is-mobile #activeCallView {
  padding: clamp(8px, 2vh, 24px) clamp(12px, 3vw, 24px) !important;
  gap: clamp(8px, 2vh, 24px) !important;
  justify-content: center !important;
}

/* Avatar - responsive sizing */
body.is-mobile .active-call-avatar-wrapper > div {
  width: clamp(64px, 18vw, 96px) !important;
  height: clamp(64px, 18vw, 96px) !important;
  font-size: clamp(20px, 5vw, 30px) !important;
}

/* Caller name */
body.is-mobile #activeCallName {
  font-size: clamp(16px, 4.5vw, 20px) !important;
}

/* Caller number */
body.is-mobile #activeCallNumber {
  font-size: clamp(12px, 3vw, 14px) !important;
}

/* Call status */
body.is-mobile #activeCallStatus {
  font-size: clamp(11px, 2.8vw, 14px) !important;
}

/* Timer - responsive */
body.is-mobile #callTimer {
  font-size: clamp(28px, 8vw, 40px) !important;
}

/* Call control buttons grid */
body.is-mobile #activeCallView .grid {
  gap: clamp(6px, 2vw, 12px) !important;
  max-width: clamp(260px, 75vw, 320px) !important;
}

/* Individual call control buttons */
body.is-mobile #activeCallView .grid > button {
  padding: clamp(8px, 2.5vw, 12px) !important;
  border-radius: clamp(10px, 3vw, 16px) !important;
}

body.is-mobile #activeCallView .grid > button i {
  font-size: clamp(16px, 4.5vw, 20px) !important;
}

body.is-mobile #activeCallView .grid > button span {
  font-size: clamp(9px, 2.5vw, 12px) !important;
}

/* Transcription and Details buttons row */
body.is-mobile #activeCallView .flex.gap-3 {
  gap: clamp(6px, 2vw, 12px) !important;
  max-width: clamp(260px, 75vw, 320px) !important;
}

body.is-mobile #activeCallView .flex.gap-3 > button {
  padding: clamp(10px, 3vw, 12px) !important;
  font-size: clamp(12px, 3.2vw, 14px) !important;
}

/* Hangup button */
body.is-mobile #hangupBtn {
  max-width: clamp(260px, 75vw, 320px) !important;
  padding: clamp(12px, 3.5vw, 16px) !important;
  font-size: clamp(14px, 3.5vw, 16px) !important;
}

/* ============================================
   HEIGHT-BASED CALL PANEL ADJUSTMENTS
   ============================================ */

/* Medium height screens (600-700px) */
@media (max-height: 700px) {
  body.is-mobile #activeCallView {
    padding: clamp(6px, 1.5vh, 16px) clamp(10px, 2.5vw, 20px) !important;
    gap: clamp(6px, 1.5vh, 16px) !important;
  }
  
  body.is-mobile .active-call-avatar-wrapper > div {
    width: clamp(56px, min(15vw, 10vh), 80px) !important;
    height: clamp(56px, min(15vw, 10vh), 80px) !important;
    font-size: clamp(18px, 4vw, 24px) !important;
  }
  
  body.is-mobile #callTimer {
    font-size: clamp(24px, min(7vw, 5vh), 32px) !important;
  }
  
  body.is-mobile #activeCallView .grid > button {
    padding: clamp(6px, 2vw, 10px) !important;
  }
  
  body.is-mobile #activeCallView .grid > button i {
    font-size: clamp(14px, 4vw, 18px) !important;
    margin-bottom: 2px !important;
  }
  
  body.is-mobile #activeCallView .grid > button span {
    font-size: clamp(8px, 2.2vw, 11px) !important;
  }
}

/* Short screens (< 600px) - very compact */
@media (max-height: 600px) {
  body.is-mobile #activeCallView {
    padding: 4px 8px !important;
    gap: 4px !important;
  }
  
  body.is-mobile .active-call-avatar-wrapper > div {
    width: clamp(48px, min(12vw, 8vh), 64px) !important;
    height: clamp(48px, min(12vw, 8vh), 64px) !important;
    font-size: clamp(16px, 3.5vw, 20px) !important;
  }
  
  body.is-mobile #activeCallName {
    font-size: clamp(14px, 3.5vw, 16px) !important;
  }
  
  body.is-mobile #activeCallNumber,
  body.is-mobile #activeCallStatus {
    font-size: clamp(10px, 2.5vw, 12px) !important;
  }
  
  body.is-mobile #callTimer {
    font-size: clamp(20px, min(5vw, 4vh), 28px) !important;
  }
  
  body.is-mobile #activeCallView .grid {
    gap: 4px !important;
  }
  
  body.is-mobile #activeCallView .grid > button {
    padding: 4px !important;
    min-height: 44px; /* Keep tappable */
  }
  
  body.is-mobile #activeCallView .grid > button i {
    font-size: clamp(12px, 3.5vw, 16px) !important;
    margin-bottom: 1px !important;
  }
  
  body.is-mobile #activeCallView .grid > button span {
    font-size: clamp(7px, 2vw, 9px) !important;
  }
  
  body.is-mobile #hangupBtn {
    padding: 10px !important;
  }
}

/* Very short screens (< 500px) - minimal UI */
@media (max-height: 500px) {
  body.is-mobile #activeCallView {
    flex-direction: row !important;
    flex-wrap: wrap !important;
    align-content: flex-start !important;
    padding: 4px !important;
    gap: 4px !important;
  }
  
  body.is-mobile #activeCallInfoSection {
    flex-direction: row !important;
    align-items: center !important;
    width: 100% !important;
    gap: 8px !important;
  }
  
  body.is-mobile #activeCallInfoSection > .flex {
    margin-bottom: 0 !important;
    gap: 8px !important;
  }
  
  body.is-mobile .active-call-avatar-wrapper > div {
    width: 40px !important;
    height: 40px !important;
    font-size: 14px !important;
  }
  
  body.is-mobile .active-call-timer-wrapper {
    margin-top: 0 !important;
  }
  
  body.is-mobile #callTimer {
    font-size: 18px !important;
  }
  
  /* 2x5 grid for very short screens */
  body.is-mobile #activeCallView .grid {
    grid-template-columns: repeat(5, 1fr) !important;
    max-width: 100% !important;
  }
  
  body.is-mobile #hangupBtn {
    max-width: 100% !important;
  }
}

/* ============================================
   RESPONSIVE DIALPAD
   Scales based on available viewport space
   ============================================ */

/* Base responsive dialpad - use clamp for fluid sizing */
body.is-mobile .dialpad-container {
  /* Scale container width based on viewport, max 320px */
  max-width: min(320px, calc(100vw - 48px));
  width: 100%;
  padding: 0 8px;
}

body.is-mobile .dialpad-grid {
  /* Fluid gap based on viewport */
  gap: clamp(8px, 3vw, 22px);
}

body.is-mobile .dialpad-btn {
  /* Fluid button size: min 56px, prefer 18vw, max 72px */
  width: clamp(56px, 18vw, 72px);
  height: clamp(56px, 18vw, 72px);
  max-width: clamp(56px, 18vw, 72px);
  max-height: clamp(56px, 18vw, 72px);
  /* Fluid font size */
  font-size: clamp(18px, 5vw, 24px);
}

body.is-mobile .dialpad-letters {
  font-size: clamp(8px, 2.5vw, 10px);
}

body.is-mobile .ios-call-btn {
  /* Scale call button too */
  width: clamp(56px, 18vw, 72px);
  height: clamp(56px, 18vw, 72px);
}

/* Small phones (< 480px width) - ensure dialpad doesn't exceed 320px */
body.is-mobile.bp-xs .dialpad-container {
  max-width: min(320px, calc(100vw - 32px));
}

body.is-mobile.bp-xs .dialpad-grid {
  gap: clamp(6px, 2.5vw, 16px);
}

body.is-mobile.bp-xs .dialpad-btn {
  width: clamp(52px, 17vw, 68px);
  height: clamp(52px, 17vw, 68px);
  max-width: clamp(52px, 17vw, 68px);
  max-height: clamp(52px, 17vw, 68px);
  font-size: clamp(16px, 4.5vw, 22px);
}

/* Short viewport adjustments (keyboard visible, small screen height) */
body.is-short-viewport #dialpadView .dialpad-btn {
  width: clamp(44px, 14vw, 56px);
  height: clamp(44px, 14vw, 56px);
  max-width: clamp(44px, 14vw, 56px);
  max-height: clamp(44px, 14vw, 56px);
  font-size: clamp(14px, 4vw, 18px);
}

body.is-short-viewport #dialpadView .dialpad-grid {
  gap: clamp(4px, 1.5vw, 8px);
}

body.is-short-viewport .dialpad-letters {
  display: none; /* Hide letters to save space */
}

body.is-short-viewport .ios-call-btn {
  width: clamp(44px, 14vw, 56px);
  height: clamp(44px, 14vw, 56px);
}

/* Height-based responsive sizing for dialpad */
/* Use min of width and height-based calculation */
@media (max-height: 700px) {
  body.is-mobile .dialpad-btn {
    width: clamp(52px, min(16vw, 9vh), 68px);
    height: clamp(52px, min(16vw, 9vh), 68px);
    max-width: clamp(52px, min(16vw, 9vh), 68px);
    max-height: clamp(52px, min(16vw, 9vh), 68px);
  }
  
  body.is-mobile .dialpad-grid {
    gap: clamp(6px, min(2vw, 1.5vh), 16px);
  }
  
  body.is-mobile .ios-call-btn {
    width: clamp(52px, min(16vw, 9vh), 68px);
    height: clamp(52px, min(16vw, 9vh), 68px);
  }
}

@media (max-height: 600px) {
  body.is-mobile .dialpad-btn {
    width: clamp(44px, min(14vw, 8vh), 56px);
    height: clamp(44px, min(14vw, 8vh), 56px);
    max-width: clamp(44px, min(14vw, 8vh), 56px);
    max-height: clamp(44px, min(14vw, 8vh), 56px);
    font-size: clamp(14px, 4vw, 18px);
  }
  
  body.is-mobile .dialpad-grid {
    gap: clamp(4px, min(1.5vw, 1vh), 10px);
  }
  
  body.is-mobile .dialpad-letters {
    display: none;
  }
  
  body.is-mobile .ios-call-btn {
    width: clamp(44px, min(14vw, 8vh), 56px);
    height: clamp(44px, min(14vw, 8vh), 56px);
  }
}

/* ============================================
   KEYBOARD VISIBLE STATE
   Added by JS when virtual keyboard is detected
   ============================================ */

body.keyboard-visible {
  /* When keyboard is visible, content area is reduced */
}

body.keyboard-visible .message-input-container {
  /* Adjust for keyboard - remove extra bottom padding */
  padding-bottom: 8px;
}

body.keyboard-visible #conversationsList {
  /* Reduce list height when keyboard is showing */
  max-height: calc(var(--vh, 1vh) * 50);
}

body.keyboard-visible #messageThread {
  /* Message thread should scroll to show input */
  flex: 1;
  min-height: 0;
}

/* Hide non-essential elements when keyboard is visible */
body.keyboard-visible .hide-on-keyboard {
  display: none !important;
}

/* ============================================
   SCROLL BEHAVIOR
   ============================================ */

/* Prevent overscroll bounce on iOS */
body.is-pwa,
body.is-ios.is-pwa {
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
}

/* Smooth scrolling for scrollable containers */
.scroll-container,
#conversationsList,
#messageThread,
#callHistoryList,
#callPanelContactsList,
#callPanelFavoritesList {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* ============================================
   VIEWPORT META TAG SUPPORT
   These styles complement the viewport meta tag:
   <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
   ============================================ */

/* Ensure inputs don't zoom on iOS (font-size >= 16px) */
body.is-ios input[type="text"],
body.is-ios input[type="email"],
body.is-ios input[type="password"],
body.is-ios input[type="tel"],
body.is-ios input[type="number"],
body.is-ios input[type="search"],
body.is-ios textarea,
body.is-ios select {
  font-size: 16px;
}

/* ============================================
   DEBUG MODE - Show viewport info
   Add body.debug-viewport to see viewport dimensions
   ============================================ */

body.debug-viewport::after {
  content: 'vh: ' attr(data-vh) ' | dvh: ' attr(data-dvh) ' | ' attr(data-dimensions);
  position: fixed;
  top: 50px;
  left: 8px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-family: monospace;
  z-index: 99999;
  pointer-events: none;
}

/* ============================================
   MOBILE HEADER ADJUSTMENTS
   Using body.is-mobile class from layout-state.js
   ============================================ */

/* Presence button sizing across all device types */
/* Desktop/Tablet: auto-size to content (label + chevron visible) */
body.is-desktop #presenceBtn,
body.is-tablet #presenceBtn {
  width: auto;
  height: auto;
  min-width: auto;
  max-width: none;
}

/* Mobile ONLY: uniform header action buttons (48x44) */
/* Using !important to override Tailwind utility classes (px-3, py-2) */
body.is-mobile #presenceBtn,
body.is-mobile #mobileSearchBtn {
  width: 48px !important;
  height: 44px !important;
  min-width: 48px !important;
  max-width: 48px !important;
  padding: 0 !important;
  justify-content: center !important;
}

body.is-mobile #phoneContainer {
  display: none !important;
}

/* Remove the space-x-2 margin when only showing dot */
body.is-mobile #presenceBtn > * {
  margin-left: 0 !important;
}

/* Hide emoji and markdown buttons on mobile */
body.is-mobile #emojiBtn,
body.is-mobile #markdownHintBtn {
  display: none;
}

