/* ==========================================================================
   MOBILE-SPECIFIC STYLES
   --------------------------------------------------------------------------
   All rules here apply at (max-width: 480px). Tailwind-style breakpoint,
   intentionally identical to the mobile-web "phone form factor" threshold.
   Components above are already responsive but several flow/layout/safe-area
   issues only show on real iOS Safari + Android Chrome handsets.
   ========================================================================== */

/* ---------- Touch interaction defaults ---------- */

/* Apple HIG: keep webkit tap highlight subtle but not invisible.
   Avoid the default gray box that overlays buttons on Android Chrome. */
html, body {
  -webkit-tap-highlight-color: rgba(124, 58, 237, 0.18);
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

/* Prevent horizontal overflow that some mobile browsers introduce
   when scaled iframes overflow their parent. */
html, body {
  overflow-x: hidden;
  overscroll-behavior-x: none;
}

/* Lock page scroll while the soft keyboard is open so the chat input bar
   stays docked to the keyboard instead of the viewport bottom. */
html.keyboard-open {
  position: fixed;
  inset: 0;
  width: 100%;
}

/* ---------- Safe area (iPhone notch + home indicator) ---------- */

/* Use env(safe-area-inset-*) to keep content out of the notch and home
   indicator zones. PWA installed to home screen also respects this. */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

@media (max-width: 480px) {
  /* Header must respect the notch area. */
  .app-header {
    padding-top: max(0px, var(--safe-top));
    height: calc(64px + var(--safe-top));
  }

  /* Toast container starts below the notch. */
  .toast-container {
    padding-top: calc(12px + var(--safe-top));
  }

  /* Bottom sheet handle and content avoid the home indicator. */
  .bottom-sheet .sheet-container {
    padding-bottom: var(--safe-bottom);
  }

  .sheet-handle {
    margin-top: calc(10px + var(--safe-top) * 0.3);
  }

  /* Chat input bar must clear the home indicator AND sit above the
     soft keyboard when the textarea is focused. visualViewport JS
     sets --keyboard-height inline so this rule stays robust. */
  .chat-input-bar {
    margin-bottom: calc(16px + var(--safe-bottom));
  }

  /* Chat-section itself gets a bottom padding so messages don't sit
     underneath the input bar when the keyboard opens. */
  .chat-section {
    padding-bottom: var(--safe-bottom);
  }

  /* Web toolbar at the top of the iframe — push down past the notch
     inside the video frame. */
  .video-section {
    padding-top: var(--safe-top);
  }

  /* Modal onboarding safe area */
  .modal-onboarding {
    padding-bottom: var(--safe-bottom);
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
  }

  /* Command palette safe area */
  .modal-command-palette {
    padding-top: calc(50px + var(--safe-top));
  }
}

/* ---------- Touch target sizing (Apple HIG 44pt minimum) ---------- */

@media (max-width: 480px) {
  /* Base icon buttons get larger tap targets. */
  .icon-btn {
    width: 44px;
    height: 44px;
  }

  /* Web navigation buttons need bigger tap areas on mobile. */
  .web-toolbar .nav-btn {
    width: 40px;
    height: 40px;
    font-size: 0.95rem;
  }

  /* URL bar add-on buttons (bookmark, submit) should be larger. */
  .url-addon {
    width: 40px !important;
    height: 40px !important;
  }

  /* Input action icons inside chat bar. */
  .input-actions .icon-btn {
    width: 40px;
    height: 40px;
  }

  /* Emoji reaction bubbles get larger so they're easy to tap. */
  .emoji-reaction-bubble {
    width: 38px;
    height: 38px;
    font-size: 1.25rem;
  }

  /* Sheet tab buttons need bigger tap area. */
  .sheet-tab-btn {
    padding: 8px 4px;
    font-size: 0.92rem;
  }

  /* Sheet close buttons. */
  .btn-sheet-close {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }

  /* Mode selection overlay buttons are full-width, so padding covers them,
     but we make them slightly shorter for thumb reach. */
  .mode-btn {
    padding: 16px 16px;
  }

  /* Tiny icon buttons inside pinned banner and chat messages. */
  .tiny-btn {
    width: 32px;
    height: 32px;
    font-size: 0.85rem;
  }

  /* Auth form button gets full width and a bigger touch area. */
  .auth-card .btn {
    min-height: 48px;
    font-size: 0.95rem;
  }
}

/* ---------- App header mobile adjustments ---------- */

@media (max-width: 480px) {
  /* Compress the header so it doesn't eat into the video area. */
  .app-header {
    height: 56px;
    padding: 0 12px;
  }

  /* Hide the WT brand title on small screens — the avatar + partner
     status is enough context. Saves horizontal space. */
  .app-title-text {
    display: none;
  }

  /* Tighten the gap between header items. */
  .header-left, .header-right {
    gap: 8px;
  }

  /* Partner info text smaller on mobile to keep partner status visible. */
  #partner-name {
    font-size: 0.8rem;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #partner-status-msg {
    font-size: 0.65rem;
  }
}

/* ---------- Video section mobile sizing ---------- */

@media (max-width: 480px) {
  /* Reduce the video section height on mobile so chat gets more room.
     32vh on a 800px phone is 256px — too small. Use a fixed smaller
     height + min so the video is still readable. */
  .video-section {
    height: 38vh;
    min-height: 220px;
    max-height: 320px;
  }

  /* Player overlay controls: stack title and favorite button tighter. */
  .player-overlay-top {
    padding: 4px 6px;
  }

  .video-title-display {
    font-size: 0.78rem;
    max-width: 70%;
  }

  /* Compress bottom controls on mobile so they don't take half the
     video frame. */
  .player-overlay-bottom {
    padding: 6px 8px;
    gap: 8px;
  }

  .progress-bar-container {
    height: 6px; /* easier to grab than 4px */
  }

  #player-seekbar {
    height: 100%;
  }

  .player-progress-wrapper {
    font-size: 0.7rem;
    gap: 6px;
  }
}

/* ---------- Web toolbar + iframe mobile adjustments ---------- */

@media (max-width: 480px) {
  /* URL bar section gets a smaller margin on mobile so chat gets more
     vertical space. */
  .url-bar-section {
    margin: 6px 10px;
    padding: 6px 10px;
    border-radius: var(--radius-md);
  }

  .url-bar-section input {
    font-size: 0.95rem; /* ≥16px prevents iOS Safari auto-zoom on focus */
    padding: 6px 10px;
  }

  /* The web toolbar inside the iframe area should have a slightly bigger
     address pill — pinch-to-zoom in the address text often gets
     misinterpreted as a double-tap on the iframe below. */
  .web-toolbar {
    padding: 8px 8px;
    gap: 4px;
    flex-shrink: 0;
  }

  /* Address display: truncate with ellipsis but also expose the full URL
     on tap via tooltip / title attribute (web-browser.js sets title). */
  .web-toolbar .web-address-display {
    font-size: 0.78rem;
    padding: 8px 12px;
    line-height: 1.3;
    -webkit-user-select: text;
    user-select: text;
    /* Use mask-image so long URLs fade at edges instead of truncating
       abruptly. */
    -webkit-mask-image: linear-gradient(to right, transparent 0, #000 12px, #000 calc(100% - 24px), transparent 100%);
            mask-image: linear-gradient(to right, transparent 0, #000 12px, #000 calc(100% - 24px), transparent 100%);
  }

  /* The web iframe must not bounce / overscroll on mobile. iOS Safari
     otherwise lets the user bounce the iframe content, which conflicts
     with our pull-to-refresh behavior. */
  .web-iframe-el {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
}

/* ---------- Chat section mobile adjustments ---------- */

@media (max-width: 480px) {
  /* Tighter padding inside the chat message list. */
  .chat-messages {
    padding: 12px 10px;
    gap: 12px;
  }

  /* Tighter header bar for chat controls. */
  .chat-controls-bar {
    padding: 8px 12px;
  }

  /* Larger tap target on the chat search button. */
  #btn-chat-search {
    width: 36px;
    height: 36px;
  }

  /* Reply preview bar more compact on mobile. */
  .reply-preview-bar {
    padding: 6px 10px;
    gap: 8px;
  }

  .reply-text {
    font-size: 0.78rem;
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  /* Chat input bar — bigger, easier to type. */
  .chat-input-bar {
    margin: 6px 10px calc(8px + var(--safe-bottom)) 10px;
    padding: 8px 10px;
    border-radius: var(--radius-lg);
    gap: 6px;
  }

  .chat-input-bar textarea {
    font-size: 1rem; /* ≥16px prevents iOS auto-zoom */
    padding: 6px 4px;
  }

  /* Message bubbles: bigger max-width on mobile since the screen is
     narrower — gives more room for content. */
  .msg-bubble-wrapper {
    max-width: 82%;
  }

  .msg-content {
    font-size: 0.95rem;
    padding: 10px 12px;
  }

  /* Smaller message avatar on mobile. */
  .msg-avatar, .msg-avatar-placeholder {
    width: 30px;
    height: 30px;
    font-size: 0.78rem;
  }

  /* Voice message player — narrower on mobile. */
  .voice-msg-player {
    min-width: 180px;
  }
}

/* ---------- Bottom sheet mobile behavior ---------- */

@media (max-width: 480px) {
  /* Sheets become near-full-height on mobile. */
  .sheet-container {
    max-height: 92vh;
    min-height: 60vh;
  }

  /* Sheet header slightly tighter on mobile. */
  .sheet-header {
    padding: 0 16px 12px 16px;
  }

  .sheet-header h3 {
    font-size: 1.05rem;
  }

  /* Sheet content less padding on mobile. */
  .sheet-content {
    padding: 14px;
  }

  /* Stat card grid already 2-col, but tighten the gap. */
  .stats-mini-grid {
    gap: 8px;
  }

  .stat-card {
    padding: 12px 10px;
  }

  .stat-value {
    font-size: 1.2rem;
  }

  /* Settings sheet items less padding. */
  .setting-item {
    padding: 12px 0;
    gap: 10px;
  }

  .setting-title {
    font-size: 0.9rem;
  }

  /* Profile sheet header sizes. */
  .profile-username-title {
    font-size: 1.3rem;
  }

  /* Onboarding card smaller. */
  .onboarding-card {
    padding: 24px 18px;
  }

  .onboarding-slides-container {
    height: 200px;
    margin-bottom: 16px;
  }

  .onboarding-illustration {
    font-size: 3rem;
    margin-bottom: 12px;
  }

  .onboarding-slide h3 {
    font-size: 1.15rem;
  }

  .onboarding-slide p {
    font-size: 0.82rem;
  }

  /* Command palette full-width on mobile. */
  .palette-container {
    max-width: none;
    max-height: 70vh;
  }
}

/* ---------- Splash screen + auth on mobile ---------- */

@media (max-width: 480px) {
  .splash-logo {
    width: 80px;
    height: 80px;
  }

  .splash-title {
    font-size: 1.8rem;
  }

  .splash-subtitle {
    font-size: 0.9rem;
  }

  .auth-card {
    padding: 24px 18px;
  }

  .auth-logo-text {
    font-size: 1.85rem;
  }

  .auth-desc {
    font-size: 0.82rem;
  }

  /* Passcode gate keypad smaller buttons to fit narrow screens. */
  .keypad {
    gap: 12px 16px;
  }

  .keypad-btn {
    width: 56px;
    height: 56px;
    font-size: 1.25rem;
  }
}

/* ---------- Mini player mobile ---------- */

@media (max-width: 480px) {
  /* Mini player slightly smaller and positioned for thumb reach. */
  .video-section.mini-player-active {
    width: 130px;
    height: 75px;
    /* Position above the chat input bar. */
    bottom: calc(120px + var(--safe-bottom));
    top: auto !important;
    right: 12px !important;
    left: auto !important;
  }
}

/* ---------- Touch gesture: pull-to-close for bottom sheets ---------- */

@media (max-width: 480px) {
  /* Bigger grab handle on mobile so it's obvious you can swipe down. */
  .sheet-handle {
    width: 60px;
    height: 5px;
    margin-top: 12px;
    background: rgba(255,255,255,0.25);
  }
}

/* ---------- iOS-specific safe-area tweaks ---------- */

@supports (-webkit-touch-callout: none) {
  /* iOS Safari only. */
  .app-header {
    /* Subtract safe-area from the top padding calculation. */
    -webkit-backdrop-filter: blur(20px);
  }

  /* iOS rubber-band scrolling on chat list is annoying. */
  .chat-messages {
    overscroll-behavior-y: contain;
  }
}

/* ---------- Swipe gesture overlay ---------- */

.web-swipe-overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 80px;
  pointer-events: none;
  z-index: 50;
  border-radius: var(--radius-md);
  transition: opacity 120ms var(--ease-standard), transform 120ms var(--ease-standard);
  opacity: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  color: #fff;
  font-weight: 700;
  will-change: opacity, transform;
}

.web-swipe-overlay[data-direction="right"] {
  left: 8px;
  background: linear-gradient(90deg, rgba(124,58,237,0.4) 0%, rgba(124,58,237,0) 100%);
  transform-origin: left center;
}

.web-swipe-overlay[data-direction="left"] {
  right: 8px;
  background: linear-gradient(270deg, rgba(124,58,237,0.4) 0%, rgba(124,58,237,0) 100%);
  transform-origin: right center;
}

.web-swipe-overlay[data-enabled="true"]::after {
  content: attr(data-arrow);
}

.web-swipe-overlay[data-direction="right"][data-enabled="true"]::after {
  content: '◀';
}

.web-swipe-overlay[data-direction="left"][data-enabled="true"]::after {
  content: '▶';
}

/* ---------- Bottom sheet drag-to-close (mobile) ---------- */

@media (max-width: 480px) {
  /* Make the sheet handle grabbable. */
  .sheet-handle {
    cursor: grab;
    touch-action: none;
  }
  .sheet-handle:active {
    cursor: grabbing;
  }
}

/* ---------- Touch ripple feedback ---------- */

/* The web standard ripple effect (.ripple class) currently uses a
   pseudo-element approach that doesn't work well on touch devices.
   On touch, just show a brief background flash instead. */
@media (max-width: 480px) {
  .ripple::after {
    display: none;
  }
  .ripple:active {
    background: rgba(255, 255, 255, 0.08) !important;
  }
}

/* ---------- High DPI / dark mode adjustments ---------- */

@media (max-width: 480px) and (prefers-color-scheme: light) {
  /* Light theme on mobile — auto-respond to system. */
  .app-layout {
    /* Keep dark layout; light theme is user-toggleable via settings. */
  }
}

/* ---------- Landscape orientation on small phones ---------- */

@media (max-width: 900px) and (orientation: landscape) {
  /* In landscape on phones, the video takes too much vertical space.
     Compact it so chat is still usable. */
  .video-section {
    height: 50vh;
    min-height: 180px;
  }

  .app-header {
    height: 48px;
  }

  /* Hide the partner info text to save horizontal space. */
  .partner-info {
    display: none;
  }
}

/* ---------- Web Mode Sizing, Swipe Overlays & Input Animations ---------- */

@media (max-width: 480px) {
  /* Web mode active - increase height so iframe has more space on mobile */
  .video-section:has(#web-player-container:not(.hidden)) {
    height: 55vh !important;
    max-height: 500px !important;
  }
}

/* Touch swipe edge zones for mobile web navigation gesture */
.web-swipe-edge {
  position: absolute;
  top: 55px; /* Below the web toolbar */
  bottom: 0;
  width: 28px;
  z-index: 100;
  background: transparent;
}
.web-swipe-edge-left {
  left: 0;
}
.web-swipe-edge-right {
  right: 0;
}

/* Touch swipe visual indicator overlay */
.web-swipe-overlay {
  position: absolute;
  top: 55px;
  bottom: 0;
  width: 60px;
  z-index: 99;
  background: linear-gradient(to right, rgba(124, 58, 237, 0.3) 0%, transparent 100%);
  opacity: 0;
  transform: scaleX(0.6);
  transform-origin: left center;
  transition: opacity 0.15s ease, transform 0.15s ease;
  pointer-events: none;
}
.web-swipe-overlay[data-direction="left"] {
  right: 0;
  background: linear-gradient(to left, rgba(124, 58, 237, 0.3) 0%, transparent 100%);
  transform-origin: right center;
}

/* URL Input address-bar click flash highlight animation */
@keyframes input-flash {
  0% { box-shadow: 0 0 0 0px rgba(124, 58, 237, 0); border-color: rgba(255, 255, 255, 0.1); }
  50% { box-shadow: 0 0 0 8px rgba(124, 58, 237, 0.5); border-color: #7c3aed; }
  100% { box-shadow: 0 0 0 0px rgba(124, 58, 237, 0); border-color: rgba(255, 255, 255, 0.1); }
}
.highlight-flash {
  animation: input-flash 1s cubic-bezier(0.25, 0.8, 0.25, 1);
}
