/* Rex Ultra mobile polish. Phone-only overrides loaded last.
   Desktop and tablet layouts above 700px are intentionally untouched. */
@media (max-width:700px){
  html,body{max-width:100%;overflow-x:hidden!important}

  /* Workspace shell: one calm primary row instead of desktop controls wrapping into several rows. */
  #workspace:not(.rex-window-active)>.workspace-head{
    display:grid!important;
    grid-template-columns:minmax(0,1fr) auto auto!important;
    align-items:center!important;
    gap:7px!important;
    min-height:64px!important;
    height:auto!important;
    padding:8px 4px 10px!important;
    margin:0!important;
    overflow:visible!important;
    background:#071321!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head>div:first-child{
    min-width:0!important;
    overflow:hidden!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head .workspace-kicker{
    display:none!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head #workspaceTitle{
    margin:0!important;
    font-size:14px!important;
    line-height:1.2!important;
    white-space:nowrap!important;
    overflow:hidden!important;
    text-overflow:ellipsis!important;
  }

  /* Secondary desktop controls belong in the mobile tool navigation, not the primary header. */
  #workspace:not(.rex-window-active)>.workspace-head>#rexWorkspaceHome,
  #workspace:not(.rex-window-active)>.workspace-head>#workspaceExpand,
  #workspace:not(.rex-window-active)>.workspace-head>#desktopConnectWorkspace,
  #workspace:not(.rex-window-active)>.workspace-head>#openConnectDirect,
  #workspace:not(.rex-window-active)>.workspace-head>#runtimeConnectionStatus{
    display:none!important;
  }

  /* BigRex becomes a first-class phone control whenever owner authorization exposes it. */
  #workspace #bigRexWorkspaceSwitch:not([hidden]),
  #workspace .rex-bigrex-switch:not([hidden]){
    display:inline-flex!important;
    align-items:center!important;
    justify-content:center!important;
    min-width:112px!important;
    min-height:40px!important;
    max-width:150px!important;
    padding:8px 11px!important;
    margin:0!important;
    border:1px solid #e0c26d!important;
    border-radius:12px!important;
    background:linear-gradient(135deg,#705414,#bd9635)!important;
    color:#fff7d6!important;
    box-shadow:0 0 18px #d8ad3a26!important;
    font-size:10px!important;
    font-weight:900!important;
    letter-spacing:.035em!important;
    line-height:1.15!important;
    white-space:normal!important;
    text-align:center!important;
    visibility:visible!important;
    opacity:1!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head>#bigRexWorkspaceSwitch:not([hidden]){
    grid-column:1/-1!important;
    grid-row:2!important;
    width:100%!important;
    max-width:none!important;
    min-height:44px!important;
    font-size:11px!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head>#closeWorkspace{
    grid-column:3!important;
    grid-row:1!important;
    width:40px!important;
    height:40px!important;
    min-width:40px!important;
    min-height:40px!important;
    padding:0!important;
    border-radius:12px!important;
    font-size:0!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head>#closeWorkspace:after{
    content:"×";
    font-size:23px;
    line-height:1;
  }

  /* Phone PC approval stays available without dominating the workspace. */
  #workspace:not(.rex-window-active)>.workspace-head #rexPhoneApprovals{
    grid-column:1/-1!important;
    grid-row:3!important;
    width:100%!important;
    max-width:none!important;
    margin:0!important;
    gap:3px!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head #rexPhoneControlToggle{
    width:100%!important;
    min-height:40px!important;
    padding:7px 11px!important;
    border-radius:10px!important;
    font-size:10px!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head #rexPhoneApprovalMessage{
    display:block!important;
    width:100%!important;
    max-width:none!important;
    min-height:13px!important;
    overflow:hidden!important;
    text-overflow:ellipsis!important;
    white-space:nowrap!important;
    font-size:9px!important;
    line-height:1.25!important;
  }
  #rexPhoneApprovals + details{display:none!important}

  /* Mode navigation is a compact swipe rail rather than a wall of pills. */
  #workspace:not(.rex-window-active)>.workspace-modes{
    display:flex!important;
    flex-wrap:nowrap!important;
    overflow-x:auto!important;
    overflow-y:hidden!important;
    gap:7px!important;
    margin:8px -2px 4px!important;
    padding:3px 2px 8px!important;
    scroll-snap-type:x proximity;
    scrollbar-width:none;
    -webkit-overflow-scrolling:touch;
  }
  #workspace:not(.rex-window-active)>.workspace-modes::-webkit-scrollbar{display:none}
  #workspace:not(.rex-window-active)>.workspace-modes>button,
  #workspace:not(.rex-window-active)>.workspace-modes>a{
    flex:0 0 auto!important;
    min-height:40px!important;
    padding:9px 12px!important;
    font-size:9px!important;
    scroll-snap-align:start;
  }

  /* Full-screen Chat: BigRex gets a dedicated row; essentials stay visible without sideways hunting. */
  #chatWorkspace.rex-chat-v8 .rex-v8-main{
    grid-template-rows:auto minmax(0,1fr) auto!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header{
    display:grid!important;
    grid-template-columns:minmax(0,1fr)!important;
    min-width:0!important;
    min-height:0!important;
    padding:7px 9px 8px!important;
    gap:6px!important;
    overflow:visible!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header-left{
    display:flex!important;
    width:100%!important;
    min-width:0!important;
    align-items:center!important;
    gap:7px!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-menu-button{
    display:flex!important;
    flex:0 0 40px!important;
    width:40px!important;
    height:40px!important;
    min-height:40px!important;
    padding:0!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right{
    display:grid!important;
    width:100%!important;
    min-width:0!important;
    max-width:100%!important;
    grid-template-columns:minmax(72px,1fr) auto auto!important;
    align-items:stretch!important;
    gap:6px!important;
    overflow:visible!important;
    padding:0!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-bigrex-switch:not([hidden]){
    grid-column:1/-1!important;
    width:100%!important;
    max-width:none!important;
    min-height:42px!important;
    margin:0!important;
    font-size:11px!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-v9-agent-select{
    grid-column:1!important;
    width:100%!important;
    max-width:none!important;
    min-width:0!important;
    min-height:38px!important;
    font-size:11px!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-v9-watch-button,
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-v10-job-button,
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-v10-desktop-button{
    display:none!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-v10-browser-button,
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-v10-workspace-return{
    display:flex!important;
    min-height:38px!important;
    padding:7px 9px!important;
    font-size:10px!important;
    white-space:nowrap!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-compose{
    padding:8px 8px max(9px,env(safe-area-inset-bottom))!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-compose-inner{
    border-radius:16px!important;
  }
  #chatWorkspace.rex-chat-v8 #workspaceChatInput{
    min-height:50px!important;
    height:58px!important;
  }

  /* Google Home: compact controls and cards use the phone width efficiently. */
  #workspace #rexHomeV7{
    width:100%!important;
    max-width:100%!important;
    padding:12px!important;
    border-radius:15px!important;
    gap:12px!important;
    overflow-x:hidden!important;
  }
  #workspace #rexHomeV7 h3,
  #workspace .rex-v7-phone h3{
    font-size:22px!important;
    line-height:1.2!important;
    margin:4px 0 6px!important;
  }
  #workspace .rex-v7-hero p,
  #workspace .rex-v7-hint{
    font-size:12px!important;
    line-height:1.5!important;
  }
  #workspace .rex-v7-controls{
    display:grid!important;
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:7px!important;
    width:100%!important;
  }
  #workspace .rex-v7-controls button,
  #workspace .rex-v7-phone button,
  #workspace #rexHomeV7>button{
    width:100%!important;
    max-width:none!important;
    min-width:0!important;
    min-height:42px!important;
    padding:8px 9px!important;
    font-size:10px!important;
    line-height:1.2!important;
    white-space:normal!important;
  }
  #workspace .rex-v7-controls button:last-child{grid-column:1/-1!important}
  #workspace .rex-v7-fields{
    grid-template-columns:minmax(0,1fr)!important;
    gap:8px!important;
  }
  #workspace .rex-v7-tiles{
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:8px!important;
  }
  #workspace .rex-v7-tile{
    min-height:0!important;
    padding:11px!important;
    border-radius:11px!important;
  }
  #workspace .rex-v7-status,
  #workspace .rex-v7-readiness{
    padding:10px!important;
    font-size:11px!important;
    line-height:1.5!important;
  }
  #workspace .rex-v7-phone{
    padding:12px!important;
    border-radius:14px!important;
  }

  /* Full-screen window manager: side rail overlays the content instead of shrinking it. */
  #workspace.rex-window-active[data-rex-window-mode]{
    --rex-window-header:54px;
    --rex-window-rail:0px!important;
  }
  #workspace.rex-window-active[data-rex-window-mode]>.workspace-head{
    padding:0 8px!important;
    gap:5px!important;
  }
  #workspace.rex-window-active.rex-sidebar-open[data-rex-window-mode]>.workspace-modes{
    width:min(238px,84vw)!important;
    z-index:8!important;
    box-shadow:16px 0 42px #000a!important;
  }
  #rexWindowControls{
    gap:4px!important;
    min-width:0!important;
  }
  #rexWindowControls button{
    min-height:36px!important;
    padding:6px 7px!important;
    font-size:9px!important;
  }
}

@media (max-width:420px){
  #workspace:not(.rex-window-active)>.workspace-head{
    grid-template-columns:minmax(0,1fr) auto auto!important;
    gap:5px!important;
  }
  #workspace:not(.rex-window-active)>.workspace-head #workspaceTitle{
    font-size:12px!important;
  }
  #workspace #bigRexWorkspaceSwitch:not([hidden]),
  #workspace .rex-bigrex-switch:not([hidden]){
    padding:7px 8px!important;
    font-size:10px!important;
  }
  #workspace #bigRexWorkspaceSwitch:not([hidden]){
    width:100%!important;
    max-width:none!important;
  }
}

@media (max-width:380px){
  #workspace .rex-v7-tiles{grid-template-columns:1fr!important}
  #workspace .rex-v7-controls{grid-template-columns:1fr!important}
  #workspace .rex-v7-controls button:last-child{grid-column:auto!important}
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right{
    grid-template-columns:minmax(68px,1fr) auto!important;
  }
  #chatWorkspace.rex-chat-v8 .rex-v8-header-right>.rex-v10-browser-button{display:none!important}
}