/*
 * Mobile frame repair.
 *
 * This file intentionally loads after every visual theme. The themes may
 * decorate the frame, but on a phone they must not change its geometry.
 */

@media (max-width: 720px) {
  html,
  body {
    width: 100%;
    height: 100%;
    min-height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
  }

  .app-shell,
  html[data-site="juhua"] .app-shell,
  html[data-site="sunflower"] .app-shell {
    position: fixed;
    top: var(--mobile-viewport-top, 0);
    right: auto;
    bottom: auto;
    left: var(--mobile-viewport-left, 0);
    width: var(--mobile-viewport-width, 100%);
    height: var(--mobile-viewport-height, 100dvh);
    min-height: 0;
    overflow: hidden;
    grid-template-rows:
      calc(56px + env(safe-area-inset-top))
      minmax(0, 1fr);
  }

  /*
   * backdrop-filter creates a containing block for fixed descendants in
   * WebKit. The nav lives inside this header in the DOM, so the header must
   * remain filter-free or the bottom nav gets pinned to the top 56px row.
   */
  .site-header,
  html[data-site="juhua"] .site-header,
  html[data-site="sunflower"] .site-header {
    position: relative;
    z-index: 20;
    min-width: 0;
    height: calc(56px + env(safe-area-inset-top));
    min-height: 0;
    padding: env(safe-area-inset-top) 12px 0;
    grid-template-columns: minmax(0, 1fr) auto;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    filter: none !important;
    transform: none !important;
  }

  .brand {
    min-width: 0;
  }

  .brand-words {
    min-width: 0;
  }

  .brand-words strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-actions {
    min-width: 0;
  }

  .module-nav,
  html[data-site="juhua"] .module-nav,
  html[data-site="sunflower"] .module-nav {
    position: fixed !important;
    top: auto !important;
    right: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    z-index: 40;
    display: flex;
    width: 100%;
    height: calc(58px + env(safe-area-inset-bottom));
    min-height: 0;
    padding:
      0
      max(8px, env(safe-area-inset-right))
      env(safe-area-inset-bottom)
      max(8px, env(safe-area-inset-left));
    align-items: center;
    justify-content: stretch;
    gap: 2px;
    transform: none !important;
    -webkit-backdrop-filter: blur(18px);
    backdrop-filter: blur(18px);
    box-sizing: border-box;
  }

  .module-tab {
    flex: 1 1 0;
    width: auto;
    min-width: 0 !important;
    height: 58px;
    padding: 0 5px;
    gap: 5px;
    font-size: 11px;
  }

  .module-tab svg {
    width: 17px;
  }

  .nav-count {
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
  }

  .main-stage {
    width: 100%;
    height: 100%;
    min-height: 0;
    padding: 0 0 calc(58px + env(safe-area-inset-bottom));
    overflow: hidden;
  }

  .module-view,
  .module-view.is-active,
  .chat-view.is-active {
    width: 100%;
    height: 100%;
    min-height: 0;
  }

  .chat-view.is-active {
    display: block;
    padding: 0 !important;
  }

  /*
   * Theme rules made the off-canvas rail position:relative. Although it was
   * visually translated away, it still occupied a row and pushed chat down.
   */
  .conversation-rail,
  html[data-site="juhua"] .conversation-rail,
  html[data-site="sunflower"] .conversation-rail {
    position: fixed !important;
    top: calc(56px + env(safe-area-inset-top)) !important;
    right: auto !important;
    bottom: calc(58px + env(safe-area-inset-bottom)) !important;
    left: 0 !important;
    z-index: 50;
    width: min(84vw, 320px);
    height: auto !important;
    min-height: 0;
    margin: 0;
    transform: translate3d(-106%, 0, 0);
  }

  .conversation-rail.is-open,
  html[data-site="juhua"] .conversation-rail.is-open,
  html[data-site="sunflower"] .conversation-rail.is-open {
    transform: translate3d(0, 0, 0);
  }

  .rail-scrim {
    position: fixed;
    inset:
      calc(56px + env(safe-area-inset-top))
      0
      calc(58px + env(safe-area-inset-bottom));
    z-index: 45;
  }

  .chat-panel,
  html[data-site="juhua"] .chat-panel,
  html[data-site="sunflower"] .chat-panel {
    position: relative;
    display: grid;
    width: 100%;
    height: 100% !important;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    grid-template-rows: auto minmax(0, 1fr) auto;
    border: 0;
    border-radius: 0;
  }

  .chat-topbar {
    min-width: 0;
    min-height: 60px;
    padding: 8px 11px;
  }

  .mobile-rail-button {
    flex: 0 0 auto;
    width: 42px;
    height: 42px;
    margin-right: 10px;
  }

  .chat-title-wrap {
    flex: 1 1 auto;
  }

  .chat-title-wrap h2 {
    max-width: 42vw;
    font-size: 18px;
  }

  .memory-status {
    flex: 0 1 auto;
    max-width: 45vw;
    min-height: 36px;
    padding: 7px 10px;
  }

  .messages {
    width: 100%;
    min-height: 0;
    padding: 18px 11px 12px;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .empty-chat {
    min-height: 100%;
    padding: 12px 0 18px;
  }

  .empty-copy {
    width: 100%;
    padding: 24px 16px;
  }

  .empty-copy h3 {
    font-size: clamp(24px, 7.4vw, 31px);
  }

  .message,
  .message.user {
    width: 100%;
    max-width: 100%;
    margin-bottom: 18px;
  }

  .composer {
    min-width: 0;
    padding: 7px 9px 9px;
  }

  .composer-paper {
    min-width: 0;
  }

  input,
  textarea,
  select,
  .composer textarea,
  .shared-composer textarea {
    font-size: 16px;
  }

  .composer textarea {
    height: 54px !important;
    min-height: 54px;
    max-height: 54px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .composer-hint {
    display: none;
  }

  .image-attach-button {
    width: 42px;
    min-width: 42px;
    min-height: 42px;
    padding: 0;
    border-radius: 13px;
  }

  .image-attach-button span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .image-attach-button svg {
    width: 20px;
    height: 20px;
  }

  .image-compose-preview {
    gap: 8px;
    padding: 10px 12px 4px;
  }

  .image-compose-item {
    width: 62px;
    height: 62px;
  }

  .message-image-gallery {
    width: min(100%, 72vw);
    border-radius: 14px;
  }

  .message-image-gallery img {
    max-height: 340px;
  }

  .message-image-gallery.count-2 img,
  .message-image-gallery.count-3 img,
  .message-image-gallery.count-4 img {
    height: 32vw;
    max-height: 170px;
  }

  .memory-view,
  .shared-view {
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /*
   * Shared chat is a workspace on phones, not a landing page. Keep its
   * identity visible in one compact strip and give the remaining height to
   * the actual conversation.
   */
  .shared-view {
    overflow: hidden;
  }

  .shared-shell,
  html[data-site="juhua"] .shared-shell,
  html[data-site="sunflower"] .shared-shell {
    display: grid;
    width: 100%;
    height: 100%;
    min-height: 0;
    margin: 0;
    padding: 8px;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    gap: 7px;
  }

  .shared-hero,
  html[data-site="juhua"] .shared-hero,
  html[data-site="sunflower"] .shared-hero {
    display: grid;
    min-height: 0;
    padding: 10px 12px;
    align-items: center;
    grid-column: 1;
    grid-row: 1;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 10px;
    border-radius: 16px;
  }

  html[data-site="sunflower"] .shared-hero {
    border-width: 2px;
  }

  .shared-hero .eyebrow,
  .shared-hero > div:first-child > p:last-child {
    display: none;
  }

  .shared-hero h1,
  html[data-site="juhua"] .shared-hero h1,
  html[data-site="sunflower"] .shared-hero h1 {
    margin: 0;
    font-size: 23px;
    line-height: 1;
    letter-spacing: 0.03em;
    white-space: nowrap;
  }

  .shared-presence,
  html[data-site="juhua"] .shared-presence,
  html[data-site="sunflower"] .shared-presence {
    display: flex;
    width: auto;
    min-width: 0;
    margin: 0;
    padding: 0;
    justify-content: flex-end;
    flex-wrap: wrap;
    background: transparent;
    border: 0;
    box-shadow: none;
    backdrop-filter: none;
    gap: 4px;
    font-size: 9px;
  }

  .shared-presence span {
    min-width: 0;
    padding: 5px 7px;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.13);
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 999px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  html[data-site="juhua"] .shared-presence span {
    background: rgba(250, 248, 235, 0.58);
    border-color: rgba(72, 91, 77, 0.16);
  }

  .presence-orb {
    width: 7px;
    height: 7px;
  }

  .shared-room-tabs,
  html[data-site="juhua"] .shared-room-tabs,
  html[data-site="sunflower"] .shared-room-tabs {
    display: grid;
    min-height: 0;
    margin: 0;
    grid-column: 1;
    grid-row: 2;
    grid-template-columns: 1fr 1fr;
    gap: 7px;
  }

  .shared-room-tab,
  html[data-site="juhua"] .shared-room-tab,
  html[data-site="sunflower"] .shared-room-tab {
    min-height: 52px;
    padding: 7px 9px;
    border-width: 1px;
    border-radius: 14px;
    gap: 8px;
    transform: none;
  }

  .room-tab-mark {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }

  .shared-room-tab strong {
    font-size: 12px;
    white-space: nowrap;
  }

  .shared-room-tab small {
    display: none;
  }

  .shared-room,
  html[data-site="juhua"] .shared-room,
  html[data-site="sunflower"] .shared-room {
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: none;
    grid-column: 1;
    grid-row: 3;
    grid-template-rows: auto minmax(0, 1fr) auto;
    border-radius: 17px;
  }

  html[data-site="sunflower"] .shared-room {
    border-width: 2px;
  }

  .shared-room-head,
  html[data-site="juhua"] .shared-room-head,
  html[data-site="sunflower"] .shared-room-head {
    min-height: 52px;
    padding: 8px 11px;
  }

  .shared-room-head .eyebrow {
    display: none;
  }

  .shared-room-head h2,
  html[data-site="juhua"] .shared-room-head h2,
  html[data-site="sunflower"] .shared-room-head h2 {
    margin: 0;
    font-size: 16px;
    line-height: 1.2;
  }

  .read-only-badge,
  .group-status {
    max-width: 40vw;
    padding: 6px 9px;
    overflow: hidden;
    font-size: 9px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .shared-messages,
  html[data-site="juhua"] .shared-messages,
  html[data-site="sunflower"] .shared-messages {
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: none;
    padding: 14px 10px 18px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .shared-empty {
    min-height: 100%;
  }

  .shared-message,
  .shared-message.sender-user {
    max-width: 100%;
    margin-bottom: 15px;
  }

  .shared-message-content {
    padding: 11px 12px;
    line-height: 1.7;
  }

  .shared-composer,
  html[data-site="juhua"] .shared-composer,
  html[data-site="sunflower"] .shared-composer {
    padding: 7px 8px;
    gap: 7px;
  }

  .shared-composer textarea {
    min-height: 42px;
    padding: 10px 11px;
  }

  .shared-composer .send-button {
    min-height: 42px;
  }

  .shared-image-compose-preview {
    padding: 2px 4px 3px;
  }

  /* WeChat-like personal conversation: AI left, user right. */
  .message,
  .message.assistant {
    width: min(88%, 720px);
    max-width: 88%;
    margin-right: auto;
    margin-left: 0;
    grid-template-columns: 30px minmax(0, 1fr);
    gap: 8px;
  }

  .message.user {
    width: min(88%, 720px);
    max-width: 88%;
    margin-right: 0;
    margin-left: auto;
    grid-template-columns: minmax(0, 1fr) 30px;
    gap: 8px;
  }

  .message.user .message-avatar {
    grid-column: 2;
    grid-row: 1;
  }

  .message.user .message-body {
    width: auto;
    max-width: 100%;
    justify-self: end;
    grid-column: 1;
    grid-row: 1;
  }

  .message.user .message-meta {
    justify-content: flex-end;
  }

  .message.assistant .message-body {
    justify-self: start;
  }

  .message-content {
    width: fit-content;
    max-width: 100%;
    border-radius: 20px;
  }

  .message.user .message-content {
    margin-left: auto;
  }

  .message-action-dialog {
    width: 100%;
    max-width: none;
    margin: auto 0 0;
    border-radius: 26px 26px 0 0;
  }

  .message-action-sheet {
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
  }

  .message-action-grid button {
    min-height: 76px;
  }

  .message-edit-dialog {
    width: calc(100% - 20px);
    max-height: calc(var(--mobile-viewport-height, 100dvh) - 20px);
  }

  .message-edit-dialog textarea {
    min-height: 170px;
    max-height: 42dvh;
    font-size: 16px;
  }

  /*
   * The bottom bar uses exactly the same geometry on both sites. Each room
   * keeps its own material instead of leaving Juhua's safe area as flat gray.
   */
  html[data-site="juhua"],
  html[data-site="juhua"] body {
    background-color: #d9ddd2;
    background-image:
      linear-gradient(rgba(236, 236, 221, 0.72), rgba(201, 209, 198, 0.84)),
      url("/assets/juhua-bg-mobile-v2.jpg");
    background-position: center bottom;
    background-size: cover;
  }

  html[data-site="juhua"] .module-nav {
    color: #405046;
    background:
      linear-gradient(90deg, rgba(236, 238, 225, 0.94), rgba(207, 215, 204, 0.96)),
      url("/assets/juhua-bg-mobile-v2.jpg") center 82% / cover no-repeat;
    border-top: 1px solid rgba(76, 94, 81, 0.28);
    box-shadow:
      0 -9px 28px rgba(59, 70, 60, 0.12),
      inset 0 1px rgba(255, 255, 255, 0.64);
  }

  html[data-site="sunflower"] .module-nav {
    color: rgba(255, 240, 194, 0.78);
    background:
      linear-gradient(90deg, rgba(38, 46, 31, 0.96), rgba(60, 52, 24, 0.96)),
      url("/assets/van-gogh-sunflowers-mobile.jpg") center 72% / cover no-repeat;
    border-top: 1px solid rgba(212, 168, 54, 0.38);
    box-shadow:
      0 -9px 28px rgba(10, 14, 8, 0.24),
      inset 0 1px rgba(255, 226, 132, 0.16);
  }

  html[data-site="juhua"] .module-tab,
  html[data-site="sunflower"] .module-tab {
    height: 58px;
    min-height: 58px;
    border: 1px solid transparent;
    border-radius: 14px 14px 0 0;
  }

  html[data-site="juhua"] .module-tab {
    color: rgba(58, 75, 65, 0.64);
  }

  html[data-site="juhua"] .module-tab.is-active {
    color: #334239;
    background:
      linear-gradient(145deg, rgba(251, 250, 239, 0.88), rgba(217, 224, 213, 0.82));
    border-color: rgba(74, 94, 79, 0.2);
    box-shadow:
      inset 0 0 0 1px rgba(255, 255, 255, 0.5),
      0 6px 18px rgba(62, 73, 63, 0.08);
  }

  html[data-site="sunflower"] .module-tab.is-active {
    color: #fff0ba;
    background:
      linear-gradient(145deg, rgba(213, 168, 52, 0.16), rgba(22, 30, 21, 0.22));
    border-color: rgba(225, 184, 71, 0.32);
    box-shadow:
      inset 0 0 0 1px rgba(30, 23, 12, 0.32),
      0 6px 18px rgba(7, 10, 6, 0.18);
  }
}
