/*
 * Mobile safety net
 * This file is deliberately loaded last.  The prototype accumulated several
 * desktop-first modules, so shared rules live here instead of scattering
 * small, competing overrides across every feature stylesheet.
 */

/* The mobile navigation script is loaded on every viewport. Keep its close
   control out of desktop layouts; it is enabled by the mobile rule below. */
.mobile-nav-close { display: none; }

@media (max-width: 900px) {
  .rail {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 110;
    display: flex !important;
    width: 100%;
    min-height: 60px;
    flex-direction: row;
    align-items: center;
    justify-content: space-around;
    gap: 2px;
    padding: 6px max(10px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    border-top: 1px solid #dfe8eb;
    background: #fff;
    box-shadow: 0 -5px 18px rgba(21, 55, 70, .08);
  }

  .rail .brand-mark { display: none; }

  .rail-btn {
    display: grid;
    flex: 1 1 0;
    place-items: center;
    width: auto;
    height: 46px;
    color: #587281;
    font-size: 20px;
  }

  .rail-btn.active { color: #087f83; background: #eaf7f6; }
  .mobile-menu-toggle { font-size: 21px; }

  .layout,
  .detail-shell-layout { padding-bottom: 76px !important; }

  .left-panel.mobile-nav-open {
    position: fixed;
    z-index: 120;
    top: 0;
    right: 0;
    bottom: 0;
    display: block !important;
    width: min(340px, calc(100vw - 36px));
    overflow-y: auto;
    padding: 58px 16px 32px;
    border: 0;
    background: #fff;
    box-shadow: -16px 0 38px rgba(16, 46, 61, .22);
  }

  .mobile-nav-active::before {
    content: '';
    position: fixed;
    z-index: 115;
    inset: 0;
    background: rgba(12, 32, 43, .38);
  }

  .mobile-nav-close {
    position: absolute;
    top: 13px;
    right: 14px;
    display: grid;
    width: 34px;
    height: 34px;
    place-items: center;
    border: 0;
    border-radius: 50%;
    background: #f1f6f7;
    color: #496675;
    font-size: 25px;
    line-height: 1;
    cursor: pointer;
  }

  html,
  body,
  #app {
    max-width: 100%;
    overflow-x: hidden;
  }

  img,
  video,
  canvas {
    max-width: 100%;
    height: auto;
  }

  .content,
  .detail-shell-layout .content,
  .ai-chat-workspace,
  .message-layout,
  .message-shell,
  .manual-log-composer,
  .manual-record-shell,
  .group-management-shell,
  .group-tools-shell {
    min-width: 0;
  }

  /* The AI workspace has a desktop-specific three-column override.  Reset it
     here so its 700px middle-column minimum cannot force a horizontal layout
     on phones and tablets. */
  .layout:has(.ai-chat-workspace) {
    width: calc(100% - 20px) !important;
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 0 !important;
  }

  .layout:has(.ai-chat-workspace) > .left-panel,
  .layout:has(.ai-chat-workspace) > .right-panel {
    display: none !important;
  }

  .layout:has(.ai-chat-workspace) > .content {
    width: 100%;
    min-width: 0;
  }

  .ai-chat-workspace {
    padding: 18px;
  }

  .ai-chat-thread {
    min-height: 220px;
    max-height: min(54dvh, 460px);
    padding-left: 0;
    padding-right: 0;
  }

  .chat-message,
  .chat-bubble,
  .chat-response,
  .chat-response table,
  .chat-response pre,
  .ai-rich-input,
  .ai-rich-input pre,
  .message-bubble,
  .message-history {
    min-width: 0;
    max-width: 100%;
  }

  .chat-bubble,
  .message-bubble,
  .chat-response,
  .ai-rich-input,
  .inline-image-token,
  .manual-attachment-copy,
  .drive-row strong {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .chat-response pre,
  .ai-rich-input pre {
    overflow: auto;
  }

  .chat-response table {
    display: block;
    overflow-x: auto;
  }

  .ai-chat-composer,
  .ai-chat-composer textarea,
  .ai-chat-composer .ai-input-tools,
  .ai-chat-composer .chat-pending-attachments,
  .message-shell .message-editor-card,
  .message-shell .message-compose,
  .message-shell .message-compose-footer,
  .message-shell .message-compose-tools {
    min-width: 0;
    max-width: 100%;
  }

  .message-shell .message-compose-footer {
    flex-wrap: wrap;
  }

  .message-shell .message-compose-tools {
    gap: 6px 14px;
    flex-basis: 100%;
  }

  .message-shell .message-send-button {
    margin-left: auto;
  }

  .message-shell .message-path-input {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .message-shell .message-path-input .message-cloud-button {
    grid-column: 1 / -1;
  }

  .modal {
    align-items: flex-start;
    overflow: auto;
    padding: 12px;
  }

  .modal-card {
    width: 100%;
    max-height: calc(100dvh - 24px);
    overflow: auto;
    padding: 18px 16px;
  }

  .version-row {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .version-row span,
  .fingerprint code {
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .drive-modal {
    align-items: flex-start !important;
    overflow: auto !important;
    padding: 12px !important;
  }

  .drive-dialog {
    width: 100% !important;
    max-height: calc(100dvh - 24px) !important;
    margin: 0 auto;
    padding: 16px !important;
    border-radius:14px !important;
    box-sizing: border-box;
  }

  .drive-dialog-head,
  .drive-toolbar {
    gap: 10px;
  }

  .drive-toolbar {
    align-items: stretch;
  }

  .drive-toolbar .drive-breadcrumbs {
    width: 100%;
  }

  .drive-row {
    min-width: 0;
  }

  .drive-row-actions {
    min-width: 0;
  }

  .drive-row-more-panel {
    z-index: 100 !important;
  }

  .drive-action-layer {
    align-items: start;
    overflow: auto;
    padding: 12px;
  }

  .drive-action-dialog {
    width: 100%;
    margin-top: 8px;
  }

  .manual-log-composer .manual-attachment-card {
    min-width: 0;
  }

  .manual-log-composer .manual-attachment-menu {
    z-index: 50;
  }

  .table-wrap,
  .member-table-wrap,
  .project-members-table-wrap {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

@media (max-width: 700px) {
  .top-stats .top-message-link {
    display: inline-flex !important;
  }

  .topbar {
    height: auto !important;
    min-height: 58px;
    flex-wrap: wrap;
    gap: 7px 10px !important;
    padding-top: 8px !important;
    padding-bottom: 8px !important;
  }

  .wordmark {
    min-width: 0;
    flex: 0 1 auto;
    font-size: 0;
  }

  .wordmark span {
    margin-left: 5px;
    font-size: 18px;
  }

  .header-group-name,
  .top-stats > :not(.avatar),
  .topbar .top-message-text {
    display: none !important;
  }

  .top-stats {
    margin-left: auto;
  }

  .search-wrap {
    order: 3;
    flex: 1 0 100%;
    width: 100%;
    max-width: none !important;
    margin-left: 0 !important;
  }

  .search-wrap .search,
  .search-wrap input,
  .search {
    width: 100%;
    max-width: none;
  }

  .layout,
  .detail-shell-layout {
    margin-top: 104px !important;
  }

  .layout {
    width: calc(100% - 20px) !important;
    padding-top: 10px;
  }

  .search-results {
    max-height: min(58dvh, 440px);
  }

  .search-result-item,
  .search-result-copy {
    min-width: 0;
  }

  .ai-chat-workspace {
    border-radius: 12px;
    padding: 14px;
  }

  .ai-chat-head {
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 8px;
    padding-bottom: 12px;
  }

  .ai-chat-head > div:first-child {
    min-width: 0;
    flex: 1 1 190px;
  }

  .ai-chat-head h2 {
    font-size: 17px;
  }

  .ai-chat-head p {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }

  .ai-chat-head-actions {
    margin-left: 0;
    width: 100%;
    justify-content: flex-end;
  }

  .chat-action {
    padding: 6px 8px;
    font-size: 12px;
  }

  .chat-bubble {
    max-width: calc(100% - 40px);
    padding: 10px 11px;
    font-size: 13px;
  }

  .ai-chat-thread {
    min-height: 190px;
    max-height: min(48dvh, 390px);
  }

  .ai-chat-composer {
    margin-top: 10px;
    padding: 9px 10px 7px;
  }

  .ai-chat-composer textarea {
    min-height: 74px;
    padding-left: 2px !important;
    padding-right: 2px !important;
  }

  .ai-chat-composer .ai-input-tools {
    gap: 2px;
    flex-wrap: wrap;
  }

  .ai-chat-composer .ai-input-tools button:not(.ai-ask) {
    padding: 6px 5px;
    font-size: 11px;
  }

  .ai-chat-composer .ai-input-tools .ai-ask {
    margin-left: auto;
  }

  .science-shortcut-popover {
    left: auto;
    right: 0;
    width: min(360px, calc(100vw - 36px));
    transform: none;
  }

  .science-shortcut-popover::after {
    left: auto;
    right: 10px;
    transform: rotate(45deg);
  }

  .formula-editor-actions {
    flex-wrap: wrap;
  }

  .formula-editor-actions button {
    flex: 1 1 auto;
  }

  .message-shell .message-compose {
    padding: 10px 12px 12px;
  }

  .message-shell .message-compose textarea {
    min-height: 78px;
    height: 96px;
    padding: 12px;
  }

  .message-shell .message-compose-footer {
    padding: 8px;
  }

  .message-shell .message-emoji-panel,
  .message-shell .message-pending-attachments,
  .message-shell .message-inline-previews,
  .message-shell .message-path-input {
    padding-left: 10px;
    padding-right: 10px;
  }

  .message-shell .message-image-preview,
  .message-shell .message-image-attachment {
    max-width: 100%;
  }

  .drive-dialog-head h2 {
    font-size: 19px;
  }

  .drive-dialog-head p {
    font-size: 12px;
  }

  .drive-toolbar {
    padding: 12px;
  }

  .drive-toolbar > button,
  .drive-toolbar > .outline,
  .drive-toolbar > .publish {
    flex: 1 1 auto;
  }

  .drive-list {
    overflow: visible !important;
  }

  .drive-row {
    padding: 11px 12px !important;
  }

  .drive-row-actions {
    flex-wrap: nowrap !important;
  }

  .drive-row-actions > a,
  .drive-row-actions > button,
  .drive-row-more summary {
    padding: 6px 8px !important;
  }

  .drive-row-more-panel {
    position: fixed !important;
    right: 20px !important;
    left: 20px;
    top: auto !important;
    bottom: 20px !important;
    min-width: 0 !important;
    max-width: none;
  }

  .drive-form {
    align-items: stretch;
    flex-wrap: wrap;
  }

  .drive-form input {
    flex-basis: 100%;
  }

  .manual-log-composer .manual-attachment-card {
    grid-template-columns: 42px minmax(0, 1fr);
    gap: 9px;
    padding: 9px;
  }

  .manual-log-composer .manual-attachment-preview {
    width: 42px;
    height: 42px;
  }

  .manual-log-composer .manual-attachment-actions {
    grid-column: 1 / -1;
    padding-left: 51px;
    justify-content: flex-start;
  }

  .manual-log-composer .manual-attachment-menu {
    right: auto;
    left: 0;
  }

  #back-to-top {
    right: 12px !important;
    bottom: 12px !important;
  }
}

@media (max-width: 420px) {
  .topbar {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }

  .wordmark span {
    font-size: 17px;
  }

  .layout {
    width: calc(100% - 16px) !important;
  }

  .ai-chat-head-actions {
    justify-content: space-between;
  }

  .manual-record-trigger,
  .chat-clear {
    min-height: 32px;
  }

  .chat-action {
    flex: 1 1 calc(50% - 4px);
    text-align: center;
  }

  .drive-dialog {
    padding: 14px !important;
  }

  .drive-close {
    width: 28px;
    height: 28px;
    flex: 0 0 28px;
  }
}
