@media (max-width: 700px), (max-width: 960px) and (max-height: 500px) and (orientation: landscape) {
  #modal {
    --sheet-x: clamp(14px, 3.8vw, 23px);
    --sheet-y: 10px;
    width: min(570px, calc(100% - 20px - env(safe-area-inset-left) - env(safe-area-inset-right)));
    max-height: calc(100vh - 2 * var(--sheet-y));
    max-height: calc(100dvh - 2 * var(--sheet-y) - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    margin: auto;
    overflow: auto;
    overscroll-behavior: contain;
    scroll-padding-block: 84px 18px;
    border-radius: 14px;
  }
  #modal .modal-header {
    position: sticky;
    top: 0;
    z-index: 3;
    align-items: flex-start;
    gap: 9px;
    padding: 14px var(--sheet-x) 12px;
    background: #181920;
  }
  #modal .modal-header > div { min-width: 0; }
  #modal .modal-header h2 { font-size: clamp(20px, 4.9vw, 25px); line-height: 1.18; }
  #modal .modal-header p, #modal .field-hint, #modal .field small, #modal .field-label { font-size: 12px; line-height: 1.45; }
  #modal .modal-close, #modal .story-controls .icon-button,
  #modal .post-header > .icon-button, #modal .post-actions .icon-button,
  #modal .comment-form .icon-button, .chat-header > .icon-button, .chat-compose .icon-button {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    flex-shrink: 0;
  }
  #modal .modal-header .modal-close { margin-top: -3px; margin-right: -4px; }
  #modal .icon-button svg, .chat-compose .icon-button svg { width: 20px; height: 20px; }
  #modal .modal-body { min-width: 0; padding: 17px var(--sheet-x) 19px; }
  #modal .modal-body.ai-lab { padding: 17px var(--sheet-x) 19px; }
  #modal .form-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 185px), 1fr)); gap: 14px; }
  #modal .field { font-size: 13px; line-height: 1.4; }
  #modal :is(.field input, .field select, .field textarea, .comment-form input),
  .chat-compose :is(input,textarea) {
    min-width: 0;
    min-height: 44px;
    font-size: 16px;
    line-height: 1.4;
  }
  #modal .field textarea { min-height: 88px; }
  #modal .input-prefix input { min-width: 0; }
  #modal .button, .chat-compose .button { min-height: 44px; font-size: 13px; }
  #modal .modal-footer { align-items: center; flex-wrap: wrap; gap: 10px 12px; }
  #modal .modal-footer > span { flex: 1 1 120px; max-width: none; font-size: 11px; line-height: 1.5; }
  #modal .modal-footer .button { flex: 1 1 auto; font-size: 13px; }
  #modal .check-label { align-items: center; min-height: 44px; font-size: 12px; }
  #modal .check-label input { width: 19px; height: 19px; margin: 0; }
  #modal .form-error { font-size: 12px; }
  #modal .form-error:empty { display: none; }
  #modal .avatar-picker { flex-wrap: wrap; gap: 7px; }
  #modal .avatar-choice { width: 49px; height: 49px; padding: 2px; }
  #modal .avatar-choice .avatar { width: 40px; height: 40px; }
  #modal .avatar-upload { flex: 1 1 175px; min-height: 44px; min-width: 0; max-width: 100%; gap: 6px; flex-wrap: nowrap; }
  #modal .avatar-upload input, #modal .upload-box input {
    max-width: 100%;
    min-width: 0;
    min-height: 44px;
    color: #bfbfcd;
    font-size: 12px;
  }
  #modal .avatar-upload input { width: 100%; }
  #modal input[type=file]::file-selector-button {
    min-height: 40px;
    padding: 6px 9px;
    border: 1px solid #62566b;
    border-radius: 6px;
    background: #302935;
    color: var(--text);
    font: inherit;
  }
  #modal .avatar-upload-preview { width: 43px; height: 43px; }
  #modal .upload-box { padding: 13px; }
  #modal .upload-box strong { font-size: 14px; }
  #modal .upload-box > span { font-size: 12px; }
  #modal .photo-options { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
  #modal .photo-option { min-width: 0; min-height: 44px; }
  #modal .photo-option span { overflow-wrap: anywhere; }
  #modal .post-upload-preview { height: auto; max-height: 36dvh; object-fit: contain; background: var(--bg); }
  #modal .post-detail-body { padding-inline: max(9px, var(--sheet-x)); }
  #modal .post-detail-body .post-image { height: auto; max-height: 45dvh; object-fit: contain; background: var(--bg); }
  #modal .post-detail-body .comments-list { max-height: none; overflow: visible; margin-block: 19px; }
  #modal .comment strong, #modal .comment p { font-size: 13px; }
  #modal .comment-form { min-width: 0; gap: 6px; }
  #modal .comment-form > input { flex: 1; }
  #modal .comment-form > .avatar { width: 28px; height: 28px; }
  #modal.story-dialog { width: min(440px, calc(100% - 16px - env(safe-area-inset-left) - env(safe-area-inset-right))); }
  .story-dialog .story-modal {
    isolation: isolate;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(42px, 1fr) auto auto;
    min-height: min(72vh, 640px);
    min-height: min(78dvh, 640px);
    touch-action: pan-y pinch-zoom;
  }
  .story-dialog .story-picture { position: absolute; inset: 0; z-index: -2; width: 100%; height: 100%; object-fit: cover; }
  .story-dialog .story-modal::after { z-index: -1; height: 100%; background: linear-gradient(180deg, #090a138c 0%, transparent 28%, #10101861 45%, #101018f5 100%); }
  .story-dialog .story-top { position: static; grid-row: 1; gap: 8px; padding: 19px 13px 8px; min-width: 0; }
  .story-dialog .story-top > span:not(.avatar) { min-width: 0; }
  .story-dialog .story-top strong { font-size: 14px; overflow-wrap: anywhere; }
  .story-dialog .story-top small { font-size: 11px; }
  .story-dialog .story-caption { position: static; grid-row: 3; min-width: 0; padding: 10px 17px 5px; overflow-wrap: anywhere; }
  .story-dialog .story-caption h2 { font-size: clamp(22px, 5vw, 31px); line-height: 1.1; }
  .story-dialog .story-caption p { max-width: 40ch; margin-bottom: 10px; font-size: 14px; line-height: 1.48; }
  .story-dialog .story-controls { position: static; grid-row: 4; padding: 4px 13px 10px; }
  .story-dialog .story-controls .icon-button { border: 1px solid #aaa5ba8a; background: #171820b3; }
  .chat-disclaimer { font-size: 12px; line-height: 1.5; }
  .chat-layout { grid-template-columns: minmax(116px, 28%) minmax(0, 1fr); min-height: 0; }
  .chat-threads { min-width: 0; }
  .chat-person { min-height: 50px; min-width: 0; padding: 8px 6px; }
  .chat-person strong { font-size: 12px; }
  .chat-person small { font-size: 10px; }
  .chat-header { padding: 6px 10px; min-width: 0; }
  .chat-header > div { min-width: 0; }
  .chat-header strong { font-size: 14px; }
  .chat-header small { font-size: 11px; }
  .chat-messages { min-height: 0; height: clamp(165px, 40dvh, 430px); max-height: none; flex: 0 1 auto; padding: 14px 12px; overscroll-behavior-y: contain; }
  .chat-welcome { padding: 8px; }
  .chat-welcome > .avatar { width: 44px; height: 44px; margin-bottom: 6px; }
  .chat-welcome small, .message-bubble small { font-size: 11px; }
  .message-bubble { font-size: 14px; }
  .chat-compose { padding: 9px 10px; align-items: center; }
  .chat-compose input { flex: 1; }
  .chat-compose .button { flex: 1; }
}
@media (max-width: 500px) {
  #modal .modal-footer > span { flex-basis: 100%; }
  .chat-layout { grid-template-columns: minmax(0, 1fr); }
  .chat-threads {
    display: flex;
    gap: 3px;
    padding: 4px;
    border: 0;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    overscroll-behavior-x: contain;
  }
  .chat-person { flex: 1 0 60px; flex-direction: column; justify-content: center; text-align: center; gap: 3px; padding: 6px 2px; width: auto; }
  .chat-person .avatar { width: 29px; height: 29px; }
  .chat-person > span:last-child { max-width: 100%; }
  .chat-person strong { max-width: 100%; white-space: normal; font-size: 11px; line-height: 1.25; }
  .chat-person small { display: none; }
}
@media (max-width: 960px) and (max-height: 500px) and (orientation: landscape) {
  #modal { --sheet-y: 5px; scroll-padding-block: 55px 10px; }
  #modal .modal-header { position: static; padding-block: 8px; }
  #modal.story-dialog { width: min(720px, calc(100% - 26px - env(safe-area-inset-left) - env(safe-area-inset-right))); max-width: 720px; }
  .story-dialog .story-modal { grid-template-columns: minmax(0,.82fr) minmax(0,1.18fr); grid-template-rows: auto 1fr auto; min-height: min(88dvh,380px); background: var(--panel); }
  .story-dialog .story-picture { position: relative; grid-column: 1; grid-row: 1/4; z-index: auto; }
  .story-dialog .story-modal::after { display: none; }
  .story-dialog .story-top, .story-dialog .story-caption, .story-dialog .story-controls { grid-column: 2; }
  .story-dialog .story-top { grid-row: 1; padding: 8px 11px 0; }
  .story-dialog .story-caption { grid-row: 2; padding: 5px 12px; align-self: center; }
  .story-dialog .story-caption h2 { margin: 6px 0; font-size: 23px; }
  .story-dialog .story-caption p { margin-bottom: 7px; font-size: 13px; }
  .story-dialog .story-controls { grid-row: 3; padding: 0 11px 8px; }
  .story-dialog .story-progress { left: 10px; right: 56%; }
  .chat-messages { height: clamp(140px,44dvh,225px); }
}
