/* Phone-only corrections for the existing cabinet. The desktop shell, palette,
   labels and event handlers stay untouched. Match the legacy drawer breakpoint. */
@media (max-width: 760px) {
  .portal-body {
    --portal-phone-target: 44px;
    --portal-phone-icon: 20px;
    --portal-phone-header-inset: env(safe-area-inset-top, 0px);
    --portal-phone-header-height: calc(68px + var(--portal-phone-header-inset));
  }

  .portal-body .portal-mobile-head {
    box-sizing: border-box;
    height: var(--portal-phone-header-height);
    padding-top: var(--portal-phone-header-inset);
    padding-left: max(18px, env(safe-area-inset-left, 0px));
    padding-right: max(18px, env(safe-area-inset-right, 0px));
    z-index: 22;
  }

  .portal-body .portal-sidebar {
    top: var(--portal-phone-header-height);
    padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
    overscroll-behavior-y: contain;
  }

  .portal-body :is(.portal-menu-toggle, .portal-profile-actions > button) {
    box-sizing: border-box;
    display: grid;
    place-items: center;
    flex: 0 0 var(--portal-phone-target);
    width: var(--portal-phone-target);
    height: var(--portal-phone-target);
    min-width: var(--portal-phone-target);
    min-height: var(--portal-phone-target);
    margin: 0;
    padding: 0;
    line-height: 1;
  }

  .portal-body .portal-menu-toggle {
    position: relative;
    font: 20px/1 system-ui, sans-serif;
  }

  /* Keep the original accessible button and its listener. The old text glyph
     is the fallback when SVG masking is unavailable or forced colors are on. */
  @supports (mask-image: url("")) or (-webkit-mask-image: url("")) {
    .portal-body .portal-menu-toggle {
      --portal-menu-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='1.8' stroke-linecap='round'%3E%3Cpath d='M4 6h16M4 12h16M4 18h16'/%3E%3C/svg%3E");
      font-size: 0;
    }

    .portal-body .portal-menu-toggle::before {
      content: "";
      position: absolute;
      inset: 0;
      width: var(--portal-phone-icon);
      height: var(--portal-phone-icon);
      margin: auto;
      background: currentColor;
      -webkit-mask: var(--portal-menu-icon) center / contain no-repeat;
      mask: var(--portal-menu-icon) center / contain no-repeat;
      pointer-events: none;
    }
  }

  @media (forced-colors: active) {
    .portal-body .portal-menu-toggle { font-size: 20px; }
    .portal-body .portal-menu-toggle::before { display: none; }
  }

  .portal-body .portal-profile-actions > button > svg {
    display: block;
    flex: none;
    width: var(--portal-phone-icon);
    height: var(--portal-phone-icon);
  }

  .portal-body :is(.portal-nav, .portal-sidebar__bottom) > a > svg {
    display: block;
    flex: none;
    width: 19px;
    height: 19px;
  }

  /* Long labels and result badges own their space. Only the tab strip scrolls
     horizontally, and a vertical swipe over it can still scroll the page. */
  .portal-body :is(.ss-tabs, .ca-tabs, .tool-studio-tabs) {
    box-sizing: border-box;
    min-width: 0;
    max-width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
  }

  .portal-body :is(.ss-tabs, .ca-tabs, .tool-studio-tabs) > button {
    flex: 0 0 auto;
    min-height: var(--portal-phone-target);
    padding-inline: 10px;
    white-space: nowrap;
  }

  .portal-body :is(.ss-tabs, .ca-tabs, .tool-studio-tabs) > button:focus-visible {
    outline-offset: -3px;
  }

  .portal-body .speech-studio .ss-tabs {
    top: var(--portal-phone-header-height);
    z-index: 18;
    scroll-margin-top: calc(var(--portal-phone-header-height) + 8px);
  }

  .portal-body .ss-tabs > [role="tab"] {
    flex-grow: 1;
    min-width: max-content;
  }

  .portal-body .ss-tabs :is([data-artifact-count], [data-results-count]):not([hidden]) {
    box-sizing: border-box;
    display: grid;
    place-items: center;
    flex: none;
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    border-radius: 6px;
    background: var(--platform-soft, var(--paper2));
    color: var(--platform-ink, var(--ink));
    font-size: 10px;
    line-height: 1;
    font-variant-numeric: tabular-nums;
  }

  .portal-body .ss-tabs :is([data-artifact-count], [data-results-count])[hidden] {
    display: none;
  }
}

@media (max-width: 760px) and (hover: none), (max-width: 760px) and (pointer: coarse) {
  .portal-body :is(.portal-menu-toggle, .portal-profile-actions > button, .portal-nav a, .portal-sidebar__bottom > a, .tool-studio-card, .st-card):hover,
  .portal-body .portal-profile-actions > button:last-child:hover,
  .portal-body .tool-studio-card:not(:disabled):hover {
    transform: none;
  }
}

@media (max-width: 760px), (max-width: 900px) and (max-height: 520px) and (pointer: coarse) {
  .portal-body:has(#chat.is-active) {
    --portal-chat-edge: max(12px, env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px));
    --portal-chat-top-gap: 8px;
  }

  .portal-body .portal-main:has(#chat.is-active) {
    margin-inline-start: 0;
    padding: var(--portal-chat-top-gap) var(--portal-chat-edge) 0;
  }

  .portal-body #chat.portal-view.is-active {
    height: calc(100dvh - var(--portal-phone-header-height) - var(--portal-chat-top-gap));
    min-height: 0;
    max-height: none;
  }

  .portal-body #chat .chat-viewport { padding-inline: 12px; }

  .portal-body #chat .chat-bottom-bar {
    padding: 8px 12px max(8px, env(safe-area-inset-bottom, 0px));
  }

  .portal-body #chat .chat-prompt-card {
    padding-inline: 12px;
  }

  .portal-body #chat .chat-textarea {
    font-size: 16px;
    max-height: 25dvh;
  }

  .portal-body #chat .chat-msg--ai:has(.chat-msg-model-badge) .chat-msg-avatar { display: none; }

  .portal-body #chat .chat-msg--ai .chat-msg-body { padding-inline: 13px; }

  .portal-body #chat .chat-msg-model-badge svg { display: block; flex: none; }

  .portal-body #chat .chat-prompt-actions { flex-wrap: wrap; gap: 8px; }

  .portal-body #chat .chat-action-pills {
    flex: 1 1 248px;
    flex-wrap: nowrap;
    gap: 6px;
  }

  .portal-body #chat .chat-picker-controls { display: flex; gap: 6px; }

  .portal-body #chat :is(.chat-model-select-wrapper, .chat-model-pill-btn, .chat-model-pill-name) { min-width: 0; }

  .portal-body #chat :is(.chat-model-pill-btn, .chat-reasoning-pill) { min-height: 44px; }

  .portal-body #chat :is(.chat-model-pill-icon, .chat-model-pill-btn > svg, .chat-reasoning-pill) { flex-shrink: 0; }

  .portal-body #chat .chat-model-pill-btn { padding-inline: 9px; }

  .portal-body #chat .chat-attach-btn > span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .portal-body #chat :is(.chat-attach-btn, .ce-settings-trigger, .chat-voice-button, .chat-send-btn, .chat-msg-act-btn):not([hidden]) {
    box-sizing: border-box;
    display: grid;
    place-items: center;
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    min-width: 44px;
    padding: 0;
    margin: 0;
    line-height: 1;
    touch-action: manipulation;
  }

  .portal-body #chat :is(.chat-attach-btn, .ce-settings-trigger, .chat-voice-button, .chat-send-btn, .chat-msg-act-btn) > svg {
    display: block;
    width: 19px;
    height: 19px;
  }

  .portal-body #chat .chat-msg-actions { gap: 4px; }

  .portal-body #chat .chat-char-counter { white-space: nowrap; font-variant-numeric: tabular-nums; }

  @media (hover: none) and (pointer: coarse) {
    .portal-body #chat :is(.chat-attach-btn, .ce-settings-trigger, .chat-voice-button, .chat-send-btn):hover { transform: none; }
  }
}

@media (min-width: 761px) and (max-width: 900px) and (max-height: 520px) and (pointer: coarse) {
  .portal-body:has(#chat.is-active) { --portal-phone-header-height: calc(52px + env(safe-area-inset-top, 0px)); }

  .portal-body:has(#chat.is-active) .portal-mobile-head {
    position: sticky;
    top: 0;
    z-index: 22;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--portal-phone-header-height);
    padding: env(safe-area-inset-top, 0px) var(--portal-chat-edge) 0;
    background: var(--paper);
    border-bottom: 1px solid var(--line);
  }

  .portal-body:has(#chat.is-active) .portal-menu-toggle {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--ink);
    color: var(--paper);
  }

  .portal-body:has(#chat.is-active) .portal-sidebar {
    top: var(--portal-phone-header-height);
    padding-inline-start: max(16px, env(safe-area-inset-left, 0px));
    transform: translateX(-100%);
  }

  .portal-body.menu-open:has(#chat.is-active) .portal-sidebar { transform: none; }
}

@media (max-width: 350px) {
  .portal-body #chat :is(.chat-viewport, .chat-bottom-bar, .chat-prompt-card) { padding-inline: 8px; }
  .portal-body #chat .chat-action-pills { display: contents; }
  .portal-body #chat .chat-model-select-wrapper { flex-basis: 100%; }
  .portal-body #chat .chat-prompt-submit-group { gap: 5px; }
}
