/* Mobile-only overlays, dialogs, DM, and touch controls. */
@media (max-width: 760px) {
  body.mobile-ui-v1 button,
  body.mobile-ui-v1 input,
  body.mobile-ui-v1 select,
  body.mobile-ui-v1 textarea { font-size: 16px; }
  body.mobile-ui-v1 button { min-height: 44px; }

  body.mobile-ui-v1 .floating-dm-button {
    right: 14px;
    bottom: calc(82px + var(--mobile-safe-bottom));
    min-height: 52px;
    border: 3px solid #10110f;
    border-radius: 0;
    background: #19a447;
    box-shadow: 5px 5px 0 #10110f;
  }
  body.mobile-ui-v1 .floating-dm-directory {
    position: fixed;
    inset: 0;
    z-index: 150;
    width: auto;
    max-height: none;
    padding: max(16px, env(safe-area-inset-top)) 14px calc(20px + var(--mobile-safe-bottom));
    border: 0;
    border-radius: 0;
    background: linear-gradient(165deg, #fff700 0 94px, #f7a8c8 94px 100%);
    box-shadow: none;
  }
  body.mobile-ui-v1 .floating-dm-head {
    min-height: 70px;
    margin-bottom: 14px;
  }
  body.mobile-ui-v1 .floating-dm-head strong { font-size: 25px; }
  body.mobile-ui-v1 .floating-dm-head button {
    min-width: 48px;
    border: 2px solid #10110f;
    background: #fff;
    color: #10110f;
    box-shadow: 3px 3px 0 #10110f;
  }
  body.mobile-ui-v1 .floating-dm-directory > .resident-directory-search {
    width: 100%;
    min-height: 52px;
    margin-bottom: 10px;
    padding: 12px;
  }
  body.mobile-ui-v1 .floating-dm-list {
    align-content: start;
    grid-auto-rows: max-content;
    max-height: calc(100dvh - 170px - var(--mobile-safe-bottom));
    padding: 0 4px 16px 0;
  }
  body.mobile-ui-v1 .floating-dm-resident {
    min-height: 64px;
    padding: 14px;
  }

  /* Full-bleed phone DM directory. The yellow masthead owns the viewport
     edge; search and resident cards keep their readable side gutters. */
  body.mobile-ui-v1.world-home-game .floating-dm-directory {
    padding: 0 !important;
    background: #f7a8c8 !important;
  }
  body.mobile-ui-v1.world-home-game .floating-dm-head {
    box-sizing: border-box !important;
    width: 100% !important;
    min-height: 118px !important;
    margin: 0 !important;
    padding: max(18px, env(safe-area-inset-top)) 20px 18px !important;
    border-bottom: 3px solid #10110f !important;
    background: #fff700 !important;
  }
  body.mobile-ui-v1.world-home-game .floating-dm-directory > .resident-directory-search {
    width: calc(100% - 28px) !important;
    margin: 18px 14px 12px !important;
  }
  body.mobile-ui-v1.world-home-game .floating-dm-list {
    width: calc(100% - 28px) !important;
    max-height: calc(100dvh - 214px - var(--mobile-safe-bottom)) !important;
    margin: 0 14px !important;
    padding: 0 0 calc(18px + var(--mobile-safe-bottom)) !important;
    background: transparent !important;
  }
  body.mobile-ui-v1.world-home-game .floating-dm-resident {
    border: 2px solid #10110f !important;
    box-shadow: none !important;
  }

  body.mobile-ui-v1.dm-chat-open .floating-dm-directory { display: none !important; }
  body.mobile-ui-v1 .chat-room {
    position: fixed !important;
    inset: 0 !important;
    z-index: 1000 !important;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    width: 100dvw;
    height: var(--em-mobile-viewport-height, 100dvh);
    max-width: none;
    max-height: var(--em-mobile-viewport-height, 100dvh);
    margin: 0;
    padding: max(8px, env(safe-area-inset-top)) 10px max(8px, env(safe-area-inset-bottom));
    overflow: hidden;
    overscroll-behavior: none;
    border: 0;
    border-radius: 0;
    background: #fffdf3;
    box-shadow: none;
    isolation: isolate;
  }
  body.mobile-ui-v1 .chat-room[hidden] { display: none; }
  body.mobile-ui-v1 .chat-heading {
    min-width: 0;
    margin: -8px -10px 0;
    padding: max(14px, env(safe-area-inset-top)) 12px 14px;
    border-bottom: 3px solid #10110f;
    background: linear-gradient(90deg,#fff700,#f7a8c8);
  }
  body.mobile-ui-v1 .chat-heading h3 {
    min-width: 0;
    font-size: 18px;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  body.mobile-ui-v1 .chat-back {
    flex: 0 0 auto;
    border: 2px solid #10110f;
    border-radius: 0;
    background: #fff700;
    box-shadow: 3px 3px 0 #10110f;
    font-weight: 900;
  }
  body.mobile-ui-v1 .chat-thread {
    min-height: 0;
    max-height: none;
    padding: 14px 4px 18px;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  body.mobile-ui-v1 .chat-message {
    max-width: 92%;
    padding: 13px 13px 28px;
    overflow-wrap: anywhere;
  }
  body.mobile-ui-v1 .chat-message > .em-mobile-message-body {
    display: block;
  }
  body.mobile-ui-v1 .chat-message > .em-mobile-message-body.em-mobile-collapsed {
    max-height: 34rem;
    overflow: hidden;
    -webkit-mask-image: linear-gradient(#000 0 88%, transparent);
    mask-image: linear-gradient(#000 0 88%, transparent);
  }
  body.mobile-ui-v1 .chat-message > .em-mobile-expand {
    display: inline-flex;
    align-items: center;
    width: auto;
    min-height: 40px;
    margin: 8px 0 0;
    padding: 0 3px;
    border: 0;
    background: transparent;
    color: #11823a;
    box-shadow: none;
    font: inherit;
    font-size: 12px;
    font-weight: 850;
    line-height: 1.2;
  }
  body.mobile-ui-v1 .chat-message.owner > .em-mobile-expand,
  body.mobile-ui-v1 .chat-message.sent > .em-mobile-expand {
    color: rgba(255,255,255,.9);
  }
  body.mobile-ui-v1 .chat-composer {
    position: relative;
    z-index: 4;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 72px;
    gap: 8px;
    margin: 0 -2px;
    padding: 10px 2px 4px;
    border-top: 3px solid #10110f;
    background: #fffdf3;
    pointer-events: auto;
  }
  body.mobile-ui-v1 .chat-input {
    position: relative;
    z-index: 5;
    width: 100%;
    min-height: 58px;
    max-height: 120px;
    padding: 12px;
    resize: none;
    overflow-y: auto;
    border: 3px solid #10110f;
    border-radius: 0;
    background: #fff;
    color: #10110f;
    caret-color: #19a447;
    pointer-events: auto;
    touch-action: manipulation;
    -webkit-user-select: text;
    user-select: text;
  }
  body.mobile-ui-v1 .chat-send {
    width: 72px;
    min-height: 58px;
    padding: 8px;
    border: 3px solid #10110f;
    border-radius: 0;
    background: #19a447;
    color: #fff;
    box-shadow: 4px 4px 0 #10110f;
  }
  body.mobile-ui-v1 .chat-note {
    padding: 5px 2px 0;
    color: #5f665f;
    font-size: 10px;
    line-height: 1.35;
  }
  /* Each message already owns its cognition disclosure. The legacy global
     console was a fifth grid row and pushed the composer outside iOS touch. */
  body.mobile-ui-v1 .chat-room > .cognition-console { display: none !important; }

  body.mobile-ui-v1 dialog,
  body.mobile-ui-v1 dialog.identity-dialog,
  body.mobile-ui-v1 dialog.funds-dialog,
  body.mobile-ui-v1 dialog.release-approval-dialog {
    position: fixed !important;
    inset: 0 !important;
    width: 100dvw !important;
    max-width: none !important;
    height: 100dvh !important;
    max-height: none !important;
    margin: 0 !important;
    padding: max(12px, env(safe-area-inset-top)) 10px calc(12px + var(--mobile-safe-bottom)) !important;
    border: 0 !important;
    border-radius: 0 !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    background: #fffdf3;
  }
  body.mobile-ui-v1 dialog::backdrop { background: rgba(16,17,15,.58); }
  body.mobile-ui-v1 dialog > form,
  body.mobile-ui-v1 dialog > section,
  body.mobile-ui-v1 .identity-form,
  body.mobile-ui-v1 .funds-shell,
  body.mobile-ui-v1 .release-approval-card {
    width: 100%;
    max-width: none;
    min-height: calc(100dvh - 24px - var(--mobile-safe-bottom));
    margin: 0;
    padding: 16px 14px calc(84px + var(--mobile-safe-bottom));
    border: 3px solid #10110f;
    border-radius: 0;
    box-shadow: 6px 6px 0 #19a447;
  }
  body.mobile-ui-v1 .identity-fields,
  body.mobile-ui-v1 .schedule-grid,
  body.mobile-ui-v1 .thinking-provision-grid,
  body.mobile-ui-v1 .event-meta,
  body.mobile-ui-v1 .diary-library {
    grid-template-columns: minmax(0, 1fr);
  }
  body.mobile-ui-v1 .diary-index {
    max-height: 180px;
    border-right: 0;
    border-bottom: 2px solid #10110f;
  }
  body.mobile-ui-v1 .identity-form-actions,
  body.mobile-ui-v1 .release-approval-actions {
    position: sticky;
    z-index: 2;
    bottom: 0;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
    gap: 8px;
    margin: 20px -14px calc(-84px - var(--mobile-safe-bottom));
    padding: 10px 14px calc(10px + var(--mobile-safe-bottom));
    border-top: 3px solid #10110f;
    background: #fff700;
  }
  body.mobile-ui-v1 .event-id,
  body.mobile-ui-v1 .behavior-command,
  body.mobile-ui-v1 [data-technical-id] { display: none; }

  /* Canonical phone DM. These selectors intentionally outrank the historical
     game-theme !important rules without changing desktop presentation. */
  body.mobile-ui-v1.world-home-game .chat-room:not([hidden]),
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-room:not([hidden]) {
    position: fixed !important;
    top: var(--em-mobile-viewport-offset, 0px) !important;
    right: 0 !important;
    bottom: auto !important;
    left: 0 !important;
    z-index: 1000 !important;
    display: grid !important;
    grid-template-rows: auto minmax(0, 1fr) auto auto !important;
    width: 100vw !important;
    height: var(--em-mobile-viewport-height, 100dvh) !important;
    min-height: 0 !important;
    max-height: var(--em-mobile-viewport-height, 100dvh) !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    overscroll-behavior: none !important;
    border: 0 !important;
    outline: 0 !important;
    background: #f7f7ed !important;
    box-shadow: none !important;
    filter: none !important;
  }
  body.mobile-ui-v1.world-home-game .chat-room[hidden],
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-room[hidden] {
    display: none !important;
  }
  body.mobile-ui-v1.world-home-game .chat-room > .chat-heading,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-room > .chat-heading {
    display: grid !important;
    grid-template-columns: auto minmax(0, 1fr) !important;
    align-items: center !important;
    gap: 10px !important;
    width: 100% !important;
    min-height: 58px !important;
    margin: 0 !important;
    padding: max(8px, env(safe-area-inset-top)) 12px 8px !important;
    border: 0 !important;
    border-bottom: 2px solid #10110f !important;
    background: linear-gradient(100deg, #fff700 0%, #ffe775 48%, #f7a8c8 100%) !important;
  }
  body.mobile-ui-v1.world-home-game .chat-heading h3,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-heading h3 {
    display: -webkit-box !important;
    min-width: 0 !important;
    margin: 0 !important;
    overflow: hidden !important;
    color: #10110f !important;
    font-size: 16px !important;
    font-weight: 900 !important;
    line-height: 1.18 !important;
    text-overflow: ellipsis !important;
    white-space: normal !important;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
  body.mobile-ui-v1.world-home-game .chat-back,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-back {
    width: auto !important;
    min-width: 78px !important;
    min-height: 40px !important;
    padding: 7px 9px !important;
    border: 2px solid #10110f !important;
    border-radius: 0 !important;
    background: #fffdf3 !important;
    color: #10110f !important;
    box-shadow: 3px 3px 0 #19a447 !important;
    font-size: 12px !important;
    font-weight: 900 !important;
  }
  body.mobile-ui-v1.world-home-game .chat-thread,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-thread {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 14px 12px 20px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    overscroll-behavior: contain !important;
    border: 0 !important;
    border-radius: 0 !important;
    background:
      linear-gradient(180deg, rgba(247,168,200,.16), transparent 28%),
      repeating-linear-gradient(0deg, transparent 0 31px, rgba(25,164,71,.05) 31px 32px),
      #f7f7ed !important;
    scroll-padding-bottom: 18px;
  }
  body.mobile-ui-v1.world-home-game .chat-thread > .empty-state {
    margin: auto !important;
    padding: 18px !important;
    border: 2px dashed #19a447 !important;
    border-radius: 0 !important;
    background: #fff !important;
    color: #4f5b52 !important;
    text-align: center;
  }
  body.mobile-ui-v1.world-home-game .chat-message,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-message {
    min-width: 0 !important;
    max-width: 86% !important;
    padding: 11px 12px 28px !important;
    border: 2px solid #10110f !important;
    border-radius: 14px !important;
    color: #10110f !important;
    font-size: 15px !important;
    line-height: 1.55 !important;
    box-shadow: 3px 3px 0 rgba(16,17,15,.16) !important;
  }
  body.mobile-ui-v1.world-home-game .chat-message.owner,
  body.mobile-ui-v1.world-home-game .chat-message.sent,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-message.owner,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-message.sent {
    align-self: flex-end !important;
    background: #19a447 !important;
    color: #fff !important;
    box-shadow: 3px 3px 0 #fff700 !important;
  }
  body.mobile-ui-v1.world-home-game .chat-message.resident,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-message.resident {
    align-self: flex-start !important;
    background: #fff !important;
    color: #10110f !important;
    box-shadow: 3px 3px 0 #f7a8c8 !important;
  }
  body.mobile-ui-v1.world-home-game .chat-message-time,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-message-time {
    right: 34px !important;
    bottom: 7px !important;
    left: 12px !important;
    max-width: calc(100% - 48px) !important;
    color: inherit !important;
    font-size: 9px !important;
    line-height: 15px !important;
    opacity: .68 !important;
  }
  body.mobile-ui-v1.world-home-game .chat-copy,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-copy {
    right: 9px !important;
    bottom: 6px !important;
    width: 22px !important;
    height: 22px !important;
    min-width: 22px !important;
    min-height: 22px !important;
    padding: 0 !important;
    color: inherit !important;
    opacity: .72 !important;
  }
  /* Expanded room visits must size from their messages. A visit is a flex
     child of the phone timeline; without an explicit non-growing basis Safari
     can stretch an open details card to the remaining scroll viewport. */
  body.mobile-ui-v1.world-home-game .visit-session,
  body.mobile-ui-v1.world-home-game.world-home-dark .visit-session {
    display: grid !important;
    flex: 0 0 auto !important;
    grid-template-rows: max-content max-content max-content !important;
    grid-auto-rows: max-content !important;
    align-content: start !important;
    gap: 5px !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    margin: 4px 0 !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-boundary {
    justify-self: center !important;
    max-width: 92% !important;
    padding: 3px 7px !important;
    border: 0 !important;
    border-radius: 0 !important;
    background: transparent !important;
    color: #6a716c !important;
    font-size: 9px !important;
    line-height: 1.35 !important;
    text-align: center !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-card,
  body.mobile-ui-v1.world-home-game.world-home-dark .visit-session-card {
    display: block !important;
    flex: 0 0 auto !important;
    align-self: start !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    border: 2px solid #19a447 !important;
    border-left-width: 5px !important;
    border-radius: 10px !important;
    background: #fff !important;
    box-shadow: none !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-card > summary,
  body.mobile-ui-v1.world-home-game.world-home-dark .visit-session-card > summary {
    min-height: 44px !important;
    padding: 10px 11px !important;
    color: #11823a !important;
    font-size: 13px !important;
    font-weight: 900 !important;
    line-height: 1.35 !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-card[open],
  body.mobile-ui-v1.world-home-game.world-home-dark .visit-session-card[open] {
    display: block !important;
    height: auto !important;
    min-height: 0 !important;
    padding-bottom: 10px !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-card[open] > summary {
    margin-bottom: 8px !important;
    border-bottom: 1px solid #d7dfd8 !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-meta {
    margin: 0 10px 8px !important;
    font-size: 9px !important;
    overflow-wrap: anywhere;
  }
  body.mobile-ui-v1.world-home-game .visit-session-message,
  body.mobile-ui-v1.world-home-game.world-home-dark .visit-session-message {
    display: block !important;
    height: auto !important;
    min-height: 0 !important;
    margin: 7px 10px 0 !important;
    padding: 9px 10px !important;
    border-radius: 9px !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-message p,
  body.mobile-ui-v1.world-home-game.world-home-dark .visit-session-message p {
    display: block !important;
    height: auto !important;
    min-height: 0 !important;
  }
  body.mobile-ui-v1.world-home-game .visit-session-message p:empty,
  body.mobile-ui-v1.world-home-game.world-home-dark .visit-session-message p:empty {
    display: none !important;
  }
  body.mobile-ui-v1.world-home-game .behavior-trace,
  body.mobile-ui-v1.world-home-game.world-home-dark .behavior-trace {
    padding: 0 !important;
    overflow: hidden !important;
    border: 2px solid #b8c4ba !important;
    border-radius: 10px !important;
    background: #fff !important;
  }
  body.mobile-ui-v1.world-home-game .behavior-trace > summary {
    min-height: 42px !important;
    padding: 10px 11px !important;
    color: #45614d !important;
    font-size: 12px !important;
  }
  body.mobile-ui-v1.world-home-game .behavior-trace[open] {
    padding-bottom: 10px !important;
  }
  body.mobile-ui-v1.world-home-game .chat-composer,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-composer {
    position: relative !important;
    z-index: 4 !important;
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) 58px !important;
    gap: 8px !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 8px 10px 7px !important;
    border: 0 !important;
    border-top: 2px solid #10110f !important;
    background: #fffdf3 !important;
  }
  body.mobile-ui-v1.world-home-game .chat-input,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-input {
    width: 100% !important;
    min-height: 46px !important;
    max-height: 104px !important;
    padding: 10px 11px !important;
    resize: none !important;
    overflow-y: auto !important;
    border: 2px solid #10110f !important;
    border-radius: 12px !important;
    background: #fff !important;
    color: #10110f !important;
    caret-color: #19a447 !important;
    box-shadow: none !important;
    font-size: 16px !important;
    line-height: 1.35 !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    touch-action: manipulation !important;
    -webkit-user-select: text !important;
    user-select: text !important;
  }
  body.mobile-ui-v1.world-home-game .chat-input::placeholder {
    color: #8b8d89 !important;
    opacity: 1 !important;
  }
  body.mobile-ui-v1.world-home-game .chat-send,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-send {
    width: 58px !important;
    min-width: 58px !important;
    min-height: 46px !important;
    padding: 6px !important;
    border: 2px solid #10110f !important;
    border-radius: 12px !important;
    background: #19a447 !important;
    color: #fff !important;
    box-shadow: 3px 3px 0 #10110f !important;
    font-size: 15px !important;
    font-weight: 900 !important;
  }
  body.mobile-ui-v1.world-home-game .chat-note,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-note {
    margin: 0 !important;
    padding: 3px 10px max(5px, env(safe-area-inset-bottom)) !important;
    overflow: hidden !important;
    background: #fffdf3 !important;
    color: #697169 !important;
    font-size: 8px !important;
    line-height: 1.2 !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }
  body.mobile-ui-v1.world-home-game .chat-room > .cognition-console,
  body.mobile-ui-v1.world-home-game.world-home-dark .chat-room > .cognition-console {
    display: none !important;
  }
}
