body.chat-body #chatView .chat-mobile-avatar,
body.chat-body #chatView .chat-mobile-controls {
  display: none;
}

@media (max-width: 980px), (hover: none) and (pointer: coarse) {
  body.chat-body #chatView .chat-header {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    align-content: center;
    flex: 0 0 auto;
    min-height: 56px;
    height: auto;
    gap: 6px;
    padding: max(5px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 5px max(10px, env(safe-area-inset-left));
    background: var(--ui-surface-1);
  }

  body.chat-body #chatView .chat-header > .chat-header-identity {
    flex: 1 1 0;
    order: 1;
    max-width: none;
    min-width: 0;
    gap: 8px;
  }

  body.chat-body #chatView .chat-header-identity .header-avatar {
    width: 34px;
    height: 34px;
    flex: 0 0 34px;
  }

  body.chat-body #chatView .chat-header-identity span {
    display: none;
  }

  body.chat-body #chatView .chat-mobile-avatar,
  body.chat-body #chatView .chat-mobile-controls {
    display: inline-grid;
    place-items: center;
    min-height: 44px;
    flex: 0 0 auto;
    margin: 0;
  }

  body.chat-body #chatView .chat-mobile-avatar {
    order: 2;
    padding-inline: 10px;
    font-size: .8rem;
  }

  body.chat-body #chatView .chat-mobile-controls {
    order: 4;
    width: 44px;
    padding: 0;
  }

  .chat-mobile-controls svg {
    width: 20px;
    height: 20px;
  }

  .chat-mobile-controls[aria-expanded="true"] svg {
    transform: rotate(180deg);
  }

  body.chat-body #chatView .chat-header #openSidebar {
    order: 0;
    width: 44px;
    height: 44px;
    justify-content: center;
    padding: 0;
  }

  body.chat-body #chatView .chat-header > .chat-actions {
    order: 5;
    width: 100%;
    margin: 0;
    padding: 2px 2px 4px;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 6px;
    overflow: auto;
    max-height: 29dvh;
  }

  body.chat-body #chatView .chat-header .chat-actions > :is(a, button):not(.hidden):not([hidden]) {
    min-height: 44px;
  }

  body.chat-body #chatView:not(.chat-mobile-controls-open) .chat-header #openSidebar,
  body.chat-body #chatView:not(.chat-mobile-controls-open) .chat-actions > :not(#stopResponseBtn, #regenerateBtn),
  body.chat-body #chatView .chat-actions > #avatarVisibilityBtn,
  body.chat-body #chatView.is-rpg-chat .chat-mobile-avatar {
    display: none;
  }

  body.chat-body #chatView:not(.chat-mobile-controls-open) .chat-header > .chat-actions {
    display: contents;
  }

  body.chat-body #chatView:not(.chat-mobile-controls-open) .chat-actions > :is(#stopResponseBtn, #regenerateBtn) {
    order: 3;
    padding-inline: 9px;
    font-size: .8rem;
  }

  body.chat-body #chatView:not(.chat-mobile-controls-open) .chat-header:has(:is(#stopResponseBtn, #regenerateBtn):not(.hidden)) .chat-mobile-avatar {
    display: none;
  }

  body.chat-body #chatView.chat-topbar-content-hidden .chat-header > :is(.chat-actions, #openSidebar) {
    opacity: 1 !important;
    transform: none;
    visibility: visible !important;
    pointer-events: auto !important;
  }

  body.chat-body #chatView .main-chat > .companion-stage {
    display: none;
  }

  body.chat-body #chatView.chat-mobile-avatar-wide:not(.is-rpg-chat) .main-chat > .companion-stage {
    display: flex !important;
    flex: 0 0 min(28dvh, 225px);
    min-height: 0;
    padding: 0 10px;
  }

  body.chat-body #chatView.chat-mobile-avatar-wide .companion-stage-card {
    width: 100%;
    height: 100%;
    grid-template-rows: minmax(0, 1fr);
    border: 1px solid var(--ui-line-soft) !important;
    border-radius: 14px !important;
  }

  body.chat-body #chatView.chat-mobile-avatar-wide .companion-stage-copy {
    display: none;
  }

  body.chat-body #chatView.chat-mobile-avatar-wide .companion-stage-art {
    width: 100%;
    height: 100%;
    min-height: 0;
    margin: 0 !important;
    max-height: none;
  }

  body.chat-body #chatView.chat-mobile-avatar-wide .companion-stage-image {
    object-fit: cover;
    object-position: center 20%;
  }

  body.chat-body #chatView.chat-mobile-avatar-wide .companion-stage-art[data-live2d-presentation="ready"]:has(.live2d-frame:not(.hidden)) .live2d-frame {
    display: block;
  }

  body.chat-body #chatView.chat-mobile-avatar-wide .companion-stage-art[data-live2d-presentation="ready"]:has(.live2d-frame:not(.hidden)) .companion-stage-image {
    visibility: hidden;
  }

  body.chat-body #chatView > #sidebar:not(.open) {
    visibility: hidden;
  }

  body.chat-body #chatView .chat-notice-global {
    position: static;
    z-index: auto;
    flex: 0 0 auto;
    align-self: center;
    width: calc(100% - 20px);
    max-height: 18dvh;
    margin: 4px 10px 0;
    padding: 8px 10px;
    overflow: auto;
    box-shadow: none;
  }

  body.chat-body #chatView .messages {
    scrollbar-gutter: auto;
  }

  body.chat-body #chatView .composer-wrap {
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }

  body.chat-body #chatView .rpg-dice-stage {
    flex-wrap: wrap;
  }

  body.chat-body #chatView .rpg-quick-replies-list {
    max-height: min(24dvh, 170px);
    overflow-y: auto;
  }

  body.chat-body :is(.rpg-story-modal-card, .rpg-roll-details-modal-card, .rpg-character-modal-card) {
    overscroll-behavior: contain;
  }

  body.chat-body .rpg-story-modal-card .rpg-story-modal-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
  }
}

@media (max-width: 980px) and (max-height: 480px) {
  body.chat-body #chatView.chat-mobile-avatar-wide:not(.is-rpg-chat) .main-chat > .companion-stage {
    flex-basis: min(22dvh, 95px);
  }
}

/* Keep priority actions visible and put secondary controls on the top layer. */
body.chat-body #chatView .chat-command-bar { gap: 8px; overflow: visible; margin-inline-start: auto; }
.chat-overflow-trigger { box-sizing: border-box; display: inline-grid; place-items: center; width: 40px; min-height: 40px; padding: 0; flex: 0 0 auto; }
.chat-overflow-glyph { font-size: 24px; font-weight: 700; line-height: 1; margin-top: -3px; }
.chat-action-popover { position: fixed; margin: 0; inset: auto; box-sizing: border-box; width: max-content; min-width: min(225px, calc(100vw - 16px)); max-width: calc(100vw - 16px); max-height: calc(100dvh - 16px); overflow: auto; padding: 8px; border: 1px solid var(--action-border, #32445d); border-radius: 14px; background: var(--panel, #111a28); color: var(--text, #edf3ff); box-shadow: 0 12px 40px #04070e9c; }
.chat-action-popover:popover-open { display: grid; gap: 4px; }
.chat-action-heading { padding: 6px 9px; font-size: .8rem; color: var(--muted, #9dacc1); }
.chat-action-popover > .btn:not([hidden]), .chat-action-row { display: flex; width: 100%; justify-content: space-between; text-align: start; align-items: center; box-sizing: border-box; min-height: 42px; padding: 7px 9px; }
.chat-action-popover > .btn:not([hidden]) { background: transparent; border-color: transparent; }
.chat-action-popover > .btn:not([hidden]):hover, .chat-action-popover > .btn:not([hidden]):focus-visible { background: var(--action-fill, #213049); }
.chat-action-popover .scene-image-delete { color: var(--danger-text, #ff9aab); }
.chat-action-links { border-top: 1px solid var(--line, #314155); display: flex; justify-content: space-between; gap: 6px; padding: 8px 9px 2px; font-size: .8rem; }
.chat-action-links a { padding: 6px 0; }
.chat-composer-usage:not([hidden]) { width: fit-content; margin: 0 0 4px auto; padding: 2px 6px; font-size: .72rem; border-color: transparent; background: transparent; }
.scene-image-actions > .scene-background-apply, .scene-image-actions > .scene-image-save { min-height: 40px; }
.scene-image-actions > .scene-image-save[aria-disabled="true"] { pointer-events: none; opacity: .55; }
@media (max-width:980px), (hover:none) and (pointer:coarse) {
  body.chat-body #chatView .chat-header > .chat-command-bar { display: flex; flex: 0 0 auto; width: auto; max-height: none; gap: 5px; overflow: visible; padding: 0; order: 3; }
  body.chat-body #chatView:not(.chat-mobile-controls-open) .chat-header > .chat-command-bar { display: flex; }
  body.chat-body #chatView:not(.chat-mobile-controls-open) .chat-command-bar > :is(#chatManagerBtn, #storyMemoryButton, #chatMobileControls) { display: inline-flex; }
  body.chat-body #chatView .chat-command-bar > .btn:not(.hidden) { padding-inline: 8px; min-height: 44px; font-size: .8rem; }
  body.chat-body #chatView .chat-command-bar #chatMobileControls { width: 44px; justify-content: center; }
  #storyMemoryButton span { display: none; }
  .chat-avatar-setting { display: none; }
}
@media (max-width:420px) {
  body.chat-body #chatView .chat-mobile-avatar { max-width: 57px; white-space: normal; line-height: 1.05; padding-inline: 3px; }
  body.chat-body #chatView .chat-header-identity { min-width: 0; }
}

body.chat-body .scene-image-actions { display: flex; justify-content: flex-end; align-items: center; gap: 6px; }
body.chat-body .scene-image-actions > :is(.scene-background-apply, .scene-image-save) { font-size: .8rem; padding: 0 9px; }
body.chat-body .chat-action-popover > .btn:not([hidden]) { color: var(--text); box-shadow: none; border-color: transparent; background: transparent; }
body.chat-body .chat-action-popover > .btn:not([hidden]):is(:hover,:focus-visible) { background: var(--action-fill, #213049); }
body.chat-body .chat-action-popover > .scene-image-delete:not([hidden]) { color: var(--danger-text, #ff9aab); }
.chat-composer-usage[hidden] { display: none !important; }
@media (max-width:980px), (hover:none) and (pointer:coarse) {
  body.chat-body #chatView .chat-header #openSidebar { display: none; }
}
