/* KOLMAR phone usability layer: compact controls, reachable forms, and a stable touch layout. */
@media (max-width: 800px) {
  html, body { overflow-x: hidden; }
  body { font-size: 14px; }
  .main-layout {
    width: 100%;
    max-width: none;
    padding: 0 8px calc(66px + env(safe-area-inset-bottom));
  }
  .topbar {
    margin: 0 -8px 10px;
    padding-inline: 8px;
    min-height: calc(52px + env(safe-area-inset-top));
  }
  .mobile-bottom-nav {
    height: calc(64px + env(safe-area-inset-bottom));
    padding-inline: 4px;
    gap: 2px;
  }
  .mobile-bottom-nav .nav-btn {
    min-height: 56px;
    padding: 3px 1px;
    gap: 1px;
    font-size: 9px;
  }
  .mobile-bottom-nav .nav-icon { font-size: 18px; }
  .content { min-width: 0; }
  .content > .view { width: 100%; min-width: 0; }
  .stories { gap: 8px; padding: 2px 0 10px; overflow-x: auto; }
  .story { flex: 0 0 58px; font-size: 10px; }
  .story-ring, .story-visual { width: 54px; height: 54px; }
  .composer, .post-card, .profile-card, .settings-card, .channel-card, .channel-hero {
    border-radius: 14px;
    padding: 11px;
  }
  .composer { margin-bottom: 10px; }
  .composer-footer { gap: 7px; align-items: stretch; flex-wrap: wrap; }
  .attachments { gap: 5px; width: 100%; overflow-x: auto; white-space: nowrap; }
  .attach-control, .attachments > span { min-height: 36px; padding-inline: 8px; font-size: 11px; }
  .composer-footer > .primary-btn { width: 100%; min-height: 42px; }
  .post-head { gap: 7px; }
  .post-content { font-size: 14px; line-height: 1.42; }
  .post-actions { gap: 4px; margin-inline: -3px; }
  .post-actions .action-btn { min-height: 34px; padding: 0 6px; font-size: 10px; }
  .profile-card { margin-bottom: 10px; }
  .profile-card--styled .profile-banner {
    min-height: clamp(150px, 42vw, 190px);
    border-radius: 14px;
  }
  .profile-card--styled .profile-top {
    gap: 8px;
    margin-top: -30px;
    padding-inline: 12px;
  }
  .profile-card--styled .profile-top .avatar.lg {
    width: 68px;
    height: 68px;
    border-width: 3px;
  }
  .profile-card--styled .profile-identity { padding-bottom: 5px; }
  .profile-card--styled .profile-banner-copy {
    right: 74px;
    bottom: 14px;
    left: 14px;
    max-height: 2.5em;
    overflow: hidden;
    font-size: clamp(15px, 4.4vw, 20px);
    line-height: 1.15;
  }
  .profile-card--styled .profile-banner-mark {
    right: 14px;
    bottom: 14px;
    width: 46px;
    height: 46px;
    border-radius: 14px;
  }
  .profile-top { gap: 10px; }
  .profile-top .avatar.lg { width: 56px; height: 56px; }
  .profile-identity .page-title { font-size: 18px; }
  .profile-stats { margin: 12px 0; }
  .profile-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
  .profile-actions button { min-height: 40px; padding-inline: 7px; font-size: 11px; }
  .messages-layout,
  .messages-layout.has-active-chat,
  .messages-layout.has-active-chat .chat,
  .chat { min-height: calc(100svh - 126px); }
  .messages-layout .chat-messages { padding: 12px 8px; gap: 10px; }
  .messages-layout .chat-form { padding: 6px; padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
  .messages-layout .chat-input { min-height: 42px; padding: 9px 10px; font-size: 16px; }
  .messages-layout .chat-form-actions { gap: 4px; }
  /* Mobile chat behaves like a messenger: only the message history scrolls. */
  .messages-layout.has-active-chat {
    height: calc(100dvh - 126px);
    min-height: 0;
    overflow: hidden;
  }
  .messages-layout.has-active-chat .chat {
    height: 100%;
    min-height: 0;
    max-height: 100%;
    overflow: hidden;
  }
  .messages-layout.has-active-chat .chat-head,
  .messages-layout.has-active-chat .chat-form {
    flex: 0 0 auto;
  }
  .messages-layout.has-active-chat .chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
  }
  .video-note-btn { width: 40px; min-height: 40px; }
  .message-bubble-wrap { max-width: 90%; }
  .modal-backdrop {
    z-index: 100;
    align-items: flex-start !important;
    padding: max(6px, env(safe-area-inset-top)) 6px max(6px, env(safe-area-inset-bottom));
    overflow-y: auto !important;
    overscroll-behavior: contain;
  }
  .modal-backdrop > .profile-editor,
  .modal-backdrop > .story-editor,
  .modal-backdrop > .connections-modal,
  .modal-backdrop > .channel-editor,
  .modal-backdrop > .channel-create-modal {
    box-sizing: border-box;
    width: 100%;
    max-width: 560px;
    max-height: calc(100svh - max(12px, env(safe-area-inset-top) + env(safe-area-inset-bottom)));
    margin: 0 auto;
    padding: 12px;
    border-radius: 14px;
    overflow-x: hidden;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
  .profile-editor .editor-head,
  .story-editor .editor-head {
    position: sticky;
    top: -12px;
    z-index: 30;
    margin: -12px -12px 11px;
    padding: 12px;
  }
  .editor-head h2 { font-size: 18px; }
  .profile-editor .form-stack,
  .story-editor .form-stack { gap: 10px; padding-bottom: 2px; }
  .profile-editor .field,
  .story-editor .field { gap: 5px; font-size: 12px; }
  .profile-editor input,
  .profile-editor textarea,
  .story-editor input,
  .story-editor textarea,
  .story-editor select { box-sizing: border-box; min-height: 42px; padding: 9px 10px; font-size: 16px; }
  .profile-editor textarea { min-height: 72px; }
  .profile-editor .upload-zone,
  .story-editor .upload-zone { min-height: 58px; padding: 9px; gap: 3px; }
  .profile-editor .upload-zone .upload-icon { font-size: 18px; }
  .profile-editor .profile-banner-tools {
    gap: 12px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: var(--panel-2);
  }
  .profile-editor .profile-banner-tools .upload-zone {
    min-height: 68px;
    align-items: flex-start;
    text-align: left;
    border-style: dashed;
    border-color: var(--purple);
  }
  /* The drawing area is intentionally taller on phones: a finger needs room to make
     controlled strokes, and the canvas must not compete with page scrolling. */
  .profile-editor .banner-canvas {
    width: 100%;
    height: clamp(180px, 52vw, 240px);
    min-height: 180px;
    border-radius: 12px;
    border: 1px solid var(--line);
    box-shadow: 0 8px 22px rgba(0,0,0,.18);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }
  .profile-editor .editor-actions,
  .story-editor .editor-actions {
    box-sizing: border-box;
    position: sticky;
    bottom: -1px;
    z-index: 31;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 6px;
    margin: 6px -12px -12px;
    padding: 10px 12px max(8px, env(safe-area-inset-bottom));
    background: linear-gradient(180deg, transparent, var(--panel) 28%);
  }
  .profile-editor .editor-actions button,
  .story-editor .editor-actions button {
    width: 100%;
    min-width: 0;
    min-height: 44px;
    padding-inline: 4px;
    font-size: 12px;
    line-height: 1.15;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  .profile-header-options { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 5px; }
  .profile-header-option { min-height: 64px; padding: 5px; font-size: 10px; }
  .profile-header-preview { height: 26px; }
  .notification-detail { font-size: 13px; }
  .video-grid { gap: 8px; }
}

@media (max-width: 380px) {
  .main-layout { padding-inline: 5px; }
  .topbar { margin-inline: -5px; padding-inline: 5px; }
  .mobile-bottom-nav .nav-btn { font-size: 8px; }
  .profile-actions { grid-template-columns: 1fr; }
  .post-actions .action-btn { font-size: 9px; }
  .modal-backdrop > .profile-editor,
  .modal-backdrop > .story-editor { padding: 10px; }
  .profile-editor .editor-head,
  .story-editor .editor-head { margin: -10px -10px 10px; padding: 10px; }
  .profile-editor .editor-actions,
  .story-editor .editor-actions { margin-inline: -10px; padding-inline: 10px; }
  .profile-editor .banner-canvas { height: 180px; min-height: 180px; }
}

/* A chat is a separate mobile screen: the page stays still while its history moves. */
@media (max-width: 800px) {
  body:has(.messages-layout.has-active-chat),
  body:has(.messages-layout.has-active-chat) .app-shell,
  body:has(.messages-layout.has-active-chat) .main-layout,
  body:has(.messages-layout.has-active-chat) .content {
    overflow: hidden;
  }

  .messages-layout.has-active-chat {
    box-sizing: border-box;
    height: calc(100dvh - 126px);
    min-height: 0;
    max-height: calc(100dvh - 126px);
    overflow: hidden;
  }

  .messages-layout.has-active-chat .chat {
    box-sizing: border-box;
    height: 100%;
    min-height: 0;
    max-height: 100%;
    overflow: hidden;
  }

  .messages-layout.has-active-chat .chat-messages {
    min-height: 0;
    max-height: none;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
  }

  .messages-layout.has-active-chat .chat-form {
    position: relative;
    flex: 0 0 auto;
  }
}

/* Keep the message composer above the app navigation. Only the conversation history scrolls. */
@media (max-width: 800px) {
  body:has(.messages-layout.has-active-chat) {
    overflow: hidden;
  }

  body:has(.messages-layout.has-active-chat) .messages-layout.has-active-chat {
    position: fixed;
    z-index: 10;
    top: calc(58px + env(safe-area-inset-top) + 14px);
    right: 8px;
    bottom: calc(76px + env(safe-area-inset-bottom) + 4px);
    left: 8px;
    width: auto;
    height: auto;
    min-height: 0;
    max-height: none;
    margin: 0;
    overflow: hidden;
  }

  body:has(.messages-layout.has-active-chat) .messages-layout.has-active-chat .chat {
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: none;
    overflow: hidden;
  }

  body:has(.messages-layout.has-active-chat) .messages-layout.has-active-chat .chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
  }

  body:has(.messages-layout.has-active-chat) .messages-layout.has-active-chat .chat-form {
    flex: 0 0 auto;
    min-height: 0;
  }
}

@media (max-width: 380px) {
  body:has(.messages-layout.has-active-chat) .messages-layout.has-active-chat {
    right: 5px;
    left: 5px;
  }
}

@media (max-width: 800px) {
  /* Keep the banner focal point visible on narrow screens. */
  .profile-card--styled .profile-banner {
    position: relative;
    min-height: clamp(168px, 48vw, 220px);
    aspect-ratio: 1.9 / 1;
    overflow: hidden;
    isolation: isolate;
    box-shadow: 0 14px 34px rgba(0, 0, 0, .28);
  }
  .profile-card--styled .profile-banner::after {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    height: 58%;
    background: linear-gradient(transparent, rgba(8, 7, 14, .76));
    content: '';
    pointer-events: none;
  }
  .profile-card--styled .profile-banner-image {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center 38%;
    transform: scale(1.015);
    transition: transform .35s ease, object-position .35s ease;
  }
  .profile-card--styled:active .profile-banner-image { transform: scale(1.04); }
  .profile-card--styled .profile-banner-copy,
  .profile-card--styled .profile-banner-mark { z-index: 1; }
}
