/* PrivChatly mobile chat module. Loaded after app.css to keep mobile app behavior predictable. */
@media screen and (max-width: 700px), (hover: none) and (pointer: coarse) {
  html.has-chat-shell * {
    -webkit-tap-highlight-color: transparent !important;
  }

  html.has-chat-shell,
  html.has-chat-shell body {
    width: 100% !important;
    height: var(--app-viewport-height, 100dvh) !important;
    min-height: var(--app-viewport-height, 100dvh) !important;
    overflow: hidden !important;
    overscroll-behavior: none !important;
    background: #f8fafc !important;
  }

  html.has-chat-shell .app-shell {
    display: block !important;
    width: 100% !important;
    height: var(--app-viewport-height, 100dvh) !important;
    min-height: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    background: #f8fafc !important;
  }

  html.has-chat-shell .chat-shell.kg-chat-shell {
    display: flex !important;
    width: 100% !important;
    height: var(--app-viewport-height, 100dvh) !important;
    max-height: var(--app-viewport-height, 100dvh) !important;
    min-height: 0 !important;
    flex-direction: column !important;
    gap: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    border: 0 !important;
    border-radius: 0 !important;
    background: #ffffff !important;
    box-shadow: none !important;
  }

  html.has-chat-shell .chat-topbar.kg-chat-header {
    position: relative !important;
    z-index: 40 !important;
    display: grid !important;
    flex: 0 0 auto !important;
    grid-template-columns: 44px 48px minmax(0, 1fr) auto !important;
    align-items: center !important;
    gap: 8px !important;
    width: 100% !important;
    min-height: calc(66px + env(safe-area-inset-top)) !important;
    padding: calc(8px + env(safe-area-inset-top)) 10px 8px !important;
    border: 0 !important;
    border-bottom: 1px solid rgba(226, 232, 240, 0.92) !important;
    border-radius: 0 !important;
    background: linear-gradient(135deg, #ecfeff 0%, #eef2ff 52%, #fff7ed 100%) !important;
    box-shadow: 0 12px 30px -30px rgba(15, 23, 42, 0.8) !important;
  }

  html.has-chat-shell .chat-topbar .back-icon,
  html.has-chat-shell .kg-chat-actions .kg-settings-link,
  html.has-chat-shell .kg-chat-actions .kg-refresh-btn {
    display: grid !important;
    width: 42px !important;
    min-width: 42px !important;
    height: 42px !important;
    min-height: 42px !important;
    place-items: center !important;
    border: 1px solid rgba(226, 232, 240, 0.88) !important;
    border-radius: 16px !important;
    background: rgba(255, 255, 255, 0.86) !important;
    box-shadow: 0 10px 22px -20px rgba(15, 23, 42, 0.7) !important;
  }

  html.has-chat-shell .chat-peer-avatar.kg-chat-peer-avatar {
    width: 48px !important;
    height: 48px !important;
    min-width: 48px !important;
    border-radius: 50% !important;
    font-size: 1rem !important;
    box-shadow: 0 10px 22px -18px rgba(15, 23, 42, 0.82) !important;
  }

  html.has-chat-shell .chat-peer-main,
  html.has-chat-shell .kg-chat-peer-details {
    min-width: 0 !important;
    overflow: hidden !important;
  }

  html.has-chat-shell .kg-chat-peer-name {
    max-width: 100% !important;
    overflow: hidden !important;
    color: #0f172a !important;
    font-size: 1rem !important;
    font-weight: 900 !important;
    line-height: 1.15 !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  html.has-chat-shell [data-peer-presence] {
    display: inline-flex !important;
    width: max-content !important;
    max-width: 100% !important;
    align-items: center !important;
    gap: 4px !important;
    margin: 5px 0 0 !important;
    overflow: hidden !important;
    border-radius: 999px !important;
    padding: 3px 7px !important;
    background: rgba(255, 255, 255, 0.74) !important;
    color: #64748b !important;
    font-size: 0.68rem !important;
    font-weight: 800 !important;
    line-height: 1.1 !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  html.has-chat-shell [data-peer-presence] i {
    display: none !important;
  }

  html.has-chat-shell [data-peer-presence].kg-peer-online i {
    display: inline-block !important;
    width: 6px !important;
    height: 6px !important;
    border-radius: 50% !important;
    background: #22c55e !important;
  }

  html.has-chat-shell .kg-chat-actions {
    display: flex !important;
    flex: 0 0 auto !important;
    gap: 6px !important;
    align-items: center !important;
  }

  html.has-chat-shell .chat-message-stage.chat-box {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
    scrollbar-width: none !important;
    border: 0 !important;
    border-radius: 0 !important;
    padding: 12px 10px 18px !important;
    scroll-padding-bottom: calc(var(--chat-composer-height, 66px) + 16px) !important;
    background-attachment: local !important;
    box-shadow: none !important;
  }

  html.has-chat-shell .chat-message-stage.chat-box::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
  }

  html.has-chat-shell .message-date-separator {
    position: sticky !important;
    top: 8px !important;
    z-index: 8 !important;
    width: max-content !important;
    max-width: calc(100vw - 32px) !important;
    margin: 8px auto 14px !important;
    border: 1px solid rgba(226, 232, 240, 0.86) !important;
    border-radius: 999px !important;
    padding: 6px 10px !important;
    background: rgba(255, 255, 255, 0.92) !important;
    color: #64748b !important;
    font-size: 0.68rem !important;
    font-weight: 900 !important;
    box-shadow: 0 12px 28px -26px rgba(15, 23, 42, 0.75) !important;
    backdrop-filter: blur(12px) !important;
  }

  html.has-chat-shell .chat-box .message-row.chat {
    display: flex !important;
    width: 100% !important;
    min-width: 0 !important;
    gap: 7px !important;
    margin: 8px 0 !important;
    align-items: flex-end !important;
  }

  html.has-chat-shell .chat-box .message-row.outgoing,
  html.has-chat-shell .chat-box .message-row.is-own {
    justify-content: flex-end !important;
  }

  html.has-chat-shell .chat-box .message-row.incoming,
  html.has-chat-shell .chat-box .message-row.is-peer {
    justify-content: flex-start !important;
  }

  html.has-chat-shell .chat-box .profile-img.kg-message-avatar {
    display: grid !important;
    width: 32px !important;
    min-width: 32px !important;
    height: 32px !important;
    place-items: center !important;
    border-radius: 50% !important;
    font-size: 0.72rem !important;
    font-weight: 900 !important;
    box-shadow: 0 10px 22px -20px rgba(15, 23, 42, 0.78) !important;
  }

  html.has-chat-shell .chat-box .chat .details {
    min-width: 0 !important;
    max-width: calc(100vw - 78px) !important;
  }

  html.has-chat-shell .chat-box .outgoing .details,
  html.has-chat-shell .chat-box .is-own .details {
    max-width: min(78vw, 320px) !important;
  }

  html.has-chat-shell .chat-box .incoming .details,
  html.has-chat-shell .chat-box .is-peer .details {
    max-width: min(80vw, 320px) !important;
  }

  html.has-chat-shell .chat-box .chat .bubble {
    display: flex !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }

  html.has-chat-shell .chat-box .message-bubble {
    position: relative !important;
    max-width: 100% !important;
    min-width: 0 !important;
    overflow-wrap: anywhere !important;
    word-break: break-word !important;
  }

  html.has-chat-shell .chat-box .message-bubble p {
    margin: 0 !important;
    font-size: 0.94rem !important;
    line-height: 1.35 !important;
  }

  html.has-chat-shell .chat-box .message-meta {
    display: inline-flex !important;
    max-width: 100% !important;
    align-items: center !important;
    justify-content: flex-end !important;
    gap: 4px !important;
    margin-top: 4px !important;
    color: rgba(100, 116, 139, 0.94) !important;
    font-size: 0.64rem !important;
    font-weight: 900 !important;
    line-height: 1 !important;
    white-space: nowrap !important;
  }

  html.has-chat-shell .chat-box .outgoing .message-meta,
  html.has-chat-shell .chat-box .is-own .message-meta {
    margin-left: auto !important;
  }

  html.has-chat-shell .chat-box .message-status {
    width: 16px !important;
    height: 16px !important;
    flex-basis: 16px !important;
    font-size: 0 !important;
  }

  html.has-chat-shell .chat-box .message-status i {
    font-size: 9px !important;
  }

  html.has-chat-shell .chat-box .media-pending-card {
    width: min(258px, calc(100vw - 92px)) !important;
    min-height: 52px !important;
    border-radius: 16px !important;
    padding: 10px 12px !important;
    gap: 4px !important;
    font-size: 0.78rem !important;
  }

  html.has-chat-shell .chat-box .media-pending-card small {
    max-width: 100% !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  html.has-chat-shell .message-action-button {
    position: absolute !important;
    top: 50% !important;
    width: 28px !important;
    height: 28px !important;
    min-width: 28px !important;
    opacity: 0 !important;
    pointer-events: none !important;
    transform: translateY(-50%) scale(0.96) !important;
    transition: opacity 140ms ease, transform 140ms ease !important;
  }

  html.has-chat-shell .message-row.is-action-selected .message-action-button,
  html.has-chat-shell .message-row:focus-within .message-action-button {
    opacity: 1 !important;
    pointer-events: auto !important;
    transform: translateY(-50%) scale(1) !important;
  }

  html.has-chat-shell .chat-box .outgoing .message-action-button,
  html.has-chat-shell .chat-box .is-own .message-action-button {
    right: calc(100% + 5px) !important;
  }

  html.has-chat-shell .chat-box .incoming .message-action-button,
  html.has-chat-shell .chat-box .is-peer .message-action-button {
    left: calc(100% + 5px) !important;
  }

  html.has-chat-shell .chat-box .message-bubble--media,
  html.has-chat-shell .chat-box .message-bubble--voice,
  html.has-chat-shell .chat-box .message-bubble.has-animated-emoji-message,
  html.has-chat-shell .chat-box .message-bubble.has-static-emoji-message,
  html.has-chat-shell .chat-box .message-bubble:has(p.is-animated-emoji-message),
  html.has-chat-shell .chat-box .message-bubble:has(p.is-static-emoji-message) {
    display: inline-grid !important;
    max-width: 100% !important;
    border: 0 !important;
    padding: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
  }

  html.has-chat-shell .chat-box .message-attachment img,
  html.has-chat-shell .chat-box .media-preview-button,
  html.has-chat-shell .chat-box .media-video-card,
  html.has-chat-shell .chat-box .media-audio-shell {
    width: min(278px, calc(100vw - 84px)) !important;
    max-width: min(278px, calc(100vw - 84px)) !important;
  }

  html.has-chat-shell .chat-box .message-attachment img,
  html.has-chat-shell .chat-box .media-preview-button img {
    display: block !important;
    height: auto !important;
    max-height: 330px !important;
    object-fit: cover !important;
    border: 0 !important;
    border-radius: 7px !important;
  }

  html.has-chat-shell .chat-box .video-message.media-video-card {
    aspect-ratio: 16 / 9 !important;
    min-height: 0 !important;
    border-radius: 8px !important;
  }

  html.has-chat-shell .chat-box .video-message.media-video-card--portrait {
    width: min(204px, calc(100vw - 118px)) !important;
    max-width: min(204px, calc(100vw - 118px)) !important;
    aspect-ratio: 9 / 16 !important;
  }

  html.has-chat-shell .chat-box .video-message.media-video-card--square {
    width: min(246px, calc(100vw - 94px)) !important;
    max-width: min(246px, calc(100vw - 94px)) !important;
    aspect-ratio: 1 / 1 !important;
  }

  html.has-chat-shell .chat-box .video-message .chat-video {
    width: 100% !important;
    height: 100% !important;
    min-height: 0 !important;
    max-height: none !important;
    object-fit: cover !important;
  }

  html.has-chat-shell .chat-box .voice-note.media-audio-shell {
    grid-template-columns: 32px minmax(0, 1fr) !important;
    gap: 7px !important;
    width: min(276px, calc(100vw - 84px)) !important;
    max-width: min(276px, calc(100vw - 84px)) !important;
    padding: 8px !important;
    border-radius: 15px !important;
  }

  html.has-chat-shell .chat-box .voice-note .voice-note-title {
    max-width: 100% !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  html.has-chat-shell .chat-box .voice-note .voice-main-row {
    grid-template-columns: 31px minmax(0, 1fr) !important;
  }

  html.has-chat-shell .chat-box .voice-note .voice-meta-row {
    grid-template-columns: auto 1fr auto 27px !important;
    min-width: 0 !important;
  }

  html.has-chat-shell .kg-chat-composer.typing-area {
    position: relative !important;
    z-index: 50 !important;
    display: grid !important;
    flex: 0 0 auto !important;
    grid-template-columns: 42px minmax(0, 1fr) 40px 40px 44px !important;
    align-items: end !important;
    gap: 6px !important;
    width: 100% !important;
    min-height: calc(60px + env(safe-area-inset-bottom)) !important;
    margin: 0 !important;
    border: 0 !important;
    border-top: 1px solid rgba(226, 232, 240, 0.94) !important;
    border-radius: 18px 18px 0 0 !important;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom)) !important;
    background: rgba(255, 255, 255, 0.96) !important;
    box-shadow: 0 -18px 42px -36px rgba(15, 23, 42, 0.78) !important;
    backdrop-filter: blur(14px) !important;
  }

  html.has-chat-shell .kg-chat-composer.typing-area.is-keyboard-open {
    border-radius: 14px 14px 0 0 !important;
    padding-bottom: 8px !important;
  }

  html.has-chat-shell .typing-area .attach-icon,
  html.has-chat-shell .typing-area .emoji-btn,
  html.has-chat-shell .typing-area .voice-btn,
  html.has-chat-shell .typing-area .send-btn {
    width: 40px !important;
    min-width: 40px !important;
    height: 40px !important;
    min-height: 40px !important;
    border-radius: 15px !important;
    touch-action: manipulation !important;
  }

  html.has-chat-shell .typing-area .attach-icon {
    width: 42px !important;
    min-width: 42px !important;
  }

  html.has-chat-shell .typing-area .send-btn {
    width: 44px !important;
    min-width: 44px !important;
  }

  html.has-chat-shell .typing-area .input-field {
    min-height: 40px !important;
    max-height: min(112px, calc(var(--app-viewport-height, 100dvh) * 0.24)) !important;
    overflow-y: auto !important;
    overscroll-behavior: contain !important;
    border: 1px solid rgba(226, 232, 240, 0.88) !important;
    border-radius: 15px !important;
    padding: 10px 12px !important;
    background: #f8fafc !important;
    color: #0f172a !important;
    font-size: 0.93rem !important;
    line-height: 1.25 !important;
    resize: none !important;
    scrollbar-width: none !important;
    caret-color: #0f766e !important;
  }

  html.has-chat-shell .typing-area .input-field::-webkit-scrollbar {
    display: none !important;
  }

  html.has-chat-shell .attachment-selected,
  html.has-chat-shell .upload-progress {
    grid-column: 1 / -1 !important;
    margin: 0 !important;
  }

  html.has-chat-shell .emoji-picker {
    position: absolute !important;
    right: 8px !important;
    bottom: calc(100% + 8px) !important;
    left: 8px !important;
    z-index: 70 !important;
    width: auto !important;
    max-height: min(310px, calc(var(--app-viewport-height, 100dvh) - var(--chat-topbar-height, 70px) - var(--chat-composer-height, 64px) - 20px)) !important;
    overflow: hidden !important;
    border: 1px solid rgba(226, 232, 240, 0.92) !important;
    border-radius: 18px !important;
    background: rgba(255, 255, 255, 0.98) !important;
    box-shadow: 0 24px 58px -34px rgba(15, 23, 42, 0.88) !important;
  }

  html.has-chat-shell .emoji-grid {
    grid-template-columns: repeat(7, minmax(0, 1fr)) !important;
    max-height: min(196px, calc(var(--app-viewport-height, 100dvh) - var(--chat-topbar-height, 70px) - var(--chat-composer-height, 64px) - 118px)) !important;
    overflow-y: auto !important;
    overscroll-behavior: contain !important;
  }

  html.has-chat-shell .voice-volume-panel {
    max-width: 100% !important;
    border-radius: 12px !important;
  }

  html.has-chat-shell .chat-shell.is-keyboard-open .chat-topbar.kg-chat-header {
    min-height: calc(62px + env(safe-area-inset-top)) !important;
    padding-bottom: 6px !important;
  }

  html.has-chat-shell .chat-shell.is-keyboard-open .chat-message-stage.chat-box {
    padding-bottom: 12px !important;
  }
}

@media screen and (max-width: 380px) {
  html.has-chat-shell .chat-topbar.kg-chat-header {
    grid-template-columns: 40px 44px minmax(0, 1fr) auto !important;
    gap: 6px !important;
    padding-inline: 8px !important;
  }

  html.has-chat-shell .chat-topbar .back-icon,
  html.has-chat-shell .kg-chat-actions .kg-settings-link,
  html.has-chat-shell .kg-chat-actions .kg-refresh-btn {
    width: 38px !important;
    min-width: 38px !important;
    height: 38px !important;
  }

  html.has-chat-shell .chat-peer-avatar.kg-chat-peer-avatar {
    width: 44px !important;
    min-width: 44px !important;
    height: 44px !important;
  }

  html.has-chat-shell .kg-chat-actions {
    gap: 4px !important;
  }

  html.has-chat-shell .kg-chat-composer.typing-area {
    grid-template-columns: 38px minmax(0, 1fr) 38px 38px 42px !important;
    gap: 5px !important;
    padding-inline: 6px !important;
  }

  html.has-chat-shell .typing-area .attach-icon,
  html.has-chat-shell .typing-area .emoji-btn,
  html.has-chat-shell .typing-area .voice-btn {
    width: 38px !important;
    min-width: 38px !important;
    height: 38px !important;
    min-height: 38px !important;
  }

  html.has-chat-shell .typing-area .send-btn {
    width: 42px !important;
    min-width: 42px !important;
    height: 38px !important;
    min-height: 38px !important;
  }

  html.has-chat-shell .chat-box .message-attachment img,
  html.has-chat-shell .chat-box .media-preview-button,
  html.has-chat-shell .chat-box .media-video-card,
  html.has-chat-shell .chat-box .media-audio-shell {
    width: min(258px, calc(100vw - 76px)) !important;
    max-width: min(258px, calc(100vw - 76px)) !important;
  }
}
